繁体   English   中英

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

[英]how to access changed attributes after fetch? 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吗?

您可以使用此change事件

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

在View的initialize函数中绑定此事件

结束了这个:

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']);
        });
    }
// ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM