如何在获取后访问更改的属性? Backbone.js的

时间:2014-04-24 06:30:31

标签: javascript json backbone.js

我使用此代码从服务器获取模型:

var id = args.model.get('id');
var options = { 
    action: 'getSubscriber', 
    address: args.model.get('address'),
    silent: true
};

new Subscriber({ id: id }, options).fetch({
    success: function(model, response) {
        console.log(response);
        console.log(model);
    }
});

response对象包含我需要的所有数据,而model存储数据不是作为其直接属性而是作为changed对象。这是错的吗? 通常我会在model.get('name')调用的帮助下访问模型属性。在这种情况下如何访问新属性?它应该是model.changed.thePropertyIwantToAccess吗?

2 个答案:

答案 0 :(得分:2)

您可以使用此change事件

    this.model.on('change', function () {
        var changedAttributes = this.model.changedAttributes();
        //Process the changed attributes
    }, this); 

将此事件绑定在视图的initialize功能

答案 1 :(得分:0)

结束了这个:

var Subscriber = Backbone.Model.extend({
    defaults: {
        id: null,
        name: null,
        status: null
        // ...
    },
    initialize: function(attributes, options) {
        var _this = this;
        this.options = options || {};                
        this.on('change', function() {
            _this.set(_this.changedAttributes()['0']);
        });
    }
// ...
相关问题