Javascript Backbone.js 集合选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8464417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:06:29  来源:igfitidea点击:

Backbone.js collection options

javascriptbackbone.jsbackbone.js-collections

提问by Jens

I have written a model/view/collection using Backbone.js. My collection uses the fetch method to load the models from a remote server. The url required for this collection needs an id like: messages/{id}. But I have found no clean way to pass options to the Collection.

我已经使用 Backbone.js 编写了一个模型/视图/集合。我的集合使用 fetch 方法从远程服务器加载模型。此集合所需的 url 需要一个 id,如:messages/{id}。但是我没有找到将选项传递给集合的干净方法。

The backbone.js view accepts options by passing it on construction: view([options]), but the collection expects a list of models upon construction: collection([models]).

Backbone.js 视图通过在构造时传递它来接受选项:view([options]),但集合在构造时需要一个模型列表:collection([models])。

What is the "cleanest" way of passing parameters/options to this collection?

将参数/选项传递给这个集合的“最干净”的方式是什么?

Shortened code:

缩短代码:

var Messages = Backbone.Collection.extend({
 model: Message,
   url: 'http://url/messages/' + id
});

回答by nrabinowitz

@Paul's answer is good, but it's also worth noting that the urlattribute can be a function. In my opinion (and it's just opinion, since the end result is the same), the code is a little more legible, if more verbose, if you set the id in initializeand the reference it in a function:

@Paul 的回答很好,但同样值得注意的是,该url属性可以是一个函数。在我看来(这只是意见,因为最终结果是相同的),如果您initialize在函数中设置 id并引用它,代码会更清晰一些,如果更冗长:

var Messages = Backbone.Collection.extend({
  initialize: function(models, options) {
    this.id = options.id;
  },
  url: function() {
    return '/messages/' + this.id;
  },
  model: Message,
});

var collection = new Messages([], { id: 2 });
collection.fetch();

Just to make sure, though - you aren't confusing the idhere with the Model id, are you? @Paul's answer, and my code above, assume that you have multiple Messagescollections, each with its own id. If the API path /messages/<id>actually refers to a message, not a set of messages, then you only need to set the urlto /messages/in the Collection, and Backbone will automatically use /messages/<id>for each model.

不过,只是为了确保 - 您不会将idhere 与 Model混淆id,是吗?@Paul 的回答以及我上面的代码假设您有多个Messages集合,每个集合都有自己的 ID。如果API路径/messages/<id>实际上指的是消息,而不是一组消息,那么你只需要设置url/messages/在Collection中,骨干会自动使用/messages/<id>每个模型。

回答by Paul

The way you have the url property declared, the value will be determined once when the browser initially loads the javascript file, and 'id' will be undefined.

您声明 url 属性的方式将在浏览器最初加载 javascript 文件时确定一次,并且 'id' 将是未定义的。

The Backbone.Collection accepts options as the second argument in its constructor, so you can pass the id value as an option, and then set the url value within the initialize function of the collection.

Backbone.Collection 在其构造函数中接受选项作为第二个参数,因此您可以将 id 值作为选项传递,然后在集合的 initialize 函数中设置 url 值。

Here's an example

这是一个例子

var Messages = Backbone.Collection.extend({
  initialize: function(models, options) {
    this.url = 'http://url/messages/' + options.id;
  },
  model: Message,
});

var collection = new Messages([], { id: 2 });
collection.fetch();