1

在以下示例中,我将如何访问在回调函数中触发“change:guid”事件的模型?

Entry = Backbone.Model.extend({
    defaults: {
        db: 'not specified',
        guid: 'not specified'
    },

    initialize: function ()
    {
        this.on("change:guid", function (e) {
            alert(" guid changed!!!!!");
            //access model here
        });
    }

});
4

1 回答 1

3

“e”应该是模型。根据文档,可以在回调中使用以下参数:“change:[attribute]”(模型、值、选项)

http://backbonejs.org/#Events-catalog

例子:

Entry = Backbone.Model.extend({
    defaults: {
        db: 'not specified',
        guid: 'not specified'
    },
    initialize: function () {
        this.on("change:guid", function (model, value, options) {            
            console.log(model);
        })
        this.set('guid', 123);
    }
});
var entry = new Entry()

如果你看一下控制台,就会有模型。试试看:

http://jsfiddle.net/r7hXS/

于 2013-07-29T18:05:58.173 回答