簡體   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