我正在检索一个 json 并想在 json 中使用一个字段(不知道正确的术语)在获取后初始化骨干模型。
看来我必须实现parse
功能,但如何?
1)您需要defaults
在模型上定义对象,设置要从请求中填充的属性;2)然后您需要过滤(那些)必要的字段并在模型的parse
方法中构造新对象:
var model = Backbone.Model.extend({
defaults: {
swordType: null
},
parse: function(data) {
// filter what you need
var swordTypeValue = null;
if(data.hasOwnProperty('swordType')) {
swordTypeValue = data.swordType;
}
// construct attributes from it
return { swordType: swordTypeValue };
}
});