在收集Backbone.js的模型 [英] Backbone.js model with collection
问题描述
我有2个型号,一个集合。 JobSummary
是一个模型, JobSummaryList
是 JobSummary
的集合项目,然后我有一个 JobSummarySnapshot
模型包含 JobSummaryList
:
I have 2 models and one collection. JobSummary
is a model, JobSummaryList
is a collection of JobSummary
items, and then I have a JobSummarySnapshot
model that contains a JobSummaryList
:
JobSummary = Backbone.Model.extend({});
JobSummaryList = Backbone.Collection.extend({
model: JobSummary
});
JobSummarySnapshot = Backbone.Model.extend({
url: '/JobSummaryList',
defaults: {
pageNumber: 1,
summaryList: new JobSummaryList()
}
});
当我称之为读取
的 JobSummarySnapshot
对象时,它得到的一切......除了当我移动通过 summaryList
收集他们都是类型的对象
,而不是 JobSummary
When I call fetch
on the JobSummarySnapshot
object, it gets everything... Except when I move through the summaryList
collection they are all of type object
and not JobSummary
.
我想这是有道理的,因为比默认
对象,它不知道其他的 summaryList
应是类型 JobSummaryList
。我可以通过每个项目,并将其转换为 JobSummary
的对象,但我希望有办法做到这一点,而不必做手工。
I suppose this makes sense since other than the defaults
object, it doesn't know that the summaryList
should be of type JobSummaryList
. I can go through each item and convert it to a JobSummary
object, but I was hoping there was a way to do it without having to do it manually.
下面是我的测试code(工作的jsfiddle这里):
Here's my test code (working jsfiddle here):
var returnData = {
pageNumber: 3,
summaryList: [
{
id: 5,
name: 'name1'},
{
id: 6,
name: 'name2'}
]
};
var fakeserver = sinon.fakeServer.create();
fakeserver.respondWith('GET', '/JobSummaryList', [200,
{
'Content-Type': 'application/json'},
JSON.stringify(returnData)]);
var callback = sinon.spy();
var summarySnapshot = new JobSummarySnapshot();
summarySnapshot.bind('change', callback);
summarySnapshot.fetch();
fakeserver.respond();
var theReturnedList = callback.getCall(0).args[0].attributes.summaryList;
_.each(theReturnedList, function(item) {
console.log('Original Item: ');
console.log(item instanceof JobSummary); // IS FALSE
var convertedItem = new JobSummary(item);
console.log('converted item: ');
console.log(convertedItem instanceof JobSummary); // IS TRUE
});
更新: 它发生,我认为我可以覆盖解析功能,并设置这种方式......我有现在这样:
UPDATE: It occurred to me that I could override the parse function and set it that way... I have this now:
JobSummarySnapshot = Backbone.Model.extend({
url: '/JobSummaryList',
defaults: {
pageNumber: 1,
summaryList: new JobSummaryList()
},
parse: function(response) {
this.set({pageNumber: response.pageNumber});
var summaryList = new JobSummaryList();
summaryList.add(response.summaryList);
this.set({summaryList: summaryList});
}
});
这工作至今。离开的问题开放,以防有人评论它....
This works so far. Leaving the question open in case someone has comment on it....
推荐答案
您解析()
函数不应该设置()
任何事情,它是一个更好的做法,只是返回的属性,骨干将设置它的照顾。例如,
Your parse()
function shouldn't set()
anything, its a better practice to just return the attributes, Backbone will take care of setting it. e.g.
parse: function(response) {
response.summaryList = new JobSummaryList(response.summaryList);
return response;
}
无论你从解析()
是的传递给设置()
。
不返回任何东西(这好像回到未定义
)是一样的调用设置(不确定)
,其中可能导致其无法通过验证,或其他一些意想不到的结果,如果您的自定义的validate()
/ 设置()
方法期望得到的对象。如果您的验证或设置()
方法因为失败,传递给<$ C $的 options.success
回调C> Backbone.Model#取()将不会被调用。
Not returning anything (which is like returning undefined
) is the same as calling set(undefined)
, which could cause it not to pass validation, or some other unexpected results if your custom validate()
/set()
methods expects to get an object. If your validation or set()
method fails because of that, the options.success
callback passed to Backbone.Model#fetch()
won't be called.
此外,为了使这更通用的,从而使设置()
荷兰国际集团从其他地方一个普通的对象(而不是仅仅从服务器响应),也影响它,您可能要重写设置()
来代替:
Also, to make this more generic, so that set()
ing to a plain object from other places (and not only from the server response) also effects it, you might want to override set()
instead:
set: function(attributes, options) {
if (attributes.summaryList !== undefined && !(attributes.summaryList instanceof JobSummaryList)) {
attributes.summaryList = new JobSummaryList(attributes.summaryList);
}
return Backbone.Model.prototype.set.call(this, attributes, options);
}
您可能还会发现骨干关系有趣的 - 这使得它更容易处理嵌套在型号的集合/模型
You might also find Backbone-relational interesting - it makes it much easier to deal with collections/models nested inside models.
修改我忘了从set()方法返回时,code现已更新
edit I forgot to return from the set() method, the code is now updated
这篇关于在收集Backbone.js的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!