您的
parse()函数应该
set()什么也不做,只是返回属性的一种更好的做法,Backbone将负责设置它。例如
parse: function(response) { response.summaryList = new JobSummaryList(response.summaryList); return response;}无论您返回什么,
parse()都会传递给
set()。
不返回任何内容(就像return一样
undefined)与调用相同
set(undefined),这可能导致它不通过验证,或者如果您的自定义
validate()/
set()方法希望获得一个对象,则可能导致其他意外结果。如果您的验证或
set()方法因此而失败,则不会
options.success调用传递给的回调
Backbone.Model#fetch()。
另外,为了使其更通用,以便
set()从其他位置(而不仅仅是从服务器响应)访问普通对象也可以实现该目的,您可能需要覆盖
set():
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);}您可能还会发现Backbone关系很有趣-
它使处理嵌套在模型中的集合/模型更加容易。
编辑 我忘了从set()方法返回,代码现在已更新



