1

我正在检索一个 json 并想在 json 中使用一个字段(不知道正确的术语)在获取后初始化骨干模型。

看来我必须实现parse功能,但如何?

4

1 回答 1

0

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 };
    }
});
于 2013-09-26T10:44:24.590 回答