English 中文(简体)
后骨和延迟销毁事件
原标题:Backbone and delayed destroy event

也有一些简单的骨干问题。 这里不是长描述, 而是一些样本代码 :

var Group = Backbone.Model.extend({

    defaults: {
        Product: new Products()
    },

    initialize: function(data){
        var _this = this;

        var products = new Products(data.Product);
        this.set( Product , products);

        this.get( Product ).each(function(product){
            product.on( destroy , function(){
                _this.trigger( change );
            }, _this);
        });

        this.bind( change , this.update, this);
    },

    update: function(){
        console.info("something changed");

        console.log(this.get( Product ).toJSON());
    },

});

因此组模型包含产品收集, 这显然包含产品。 在初始化过程中, 我试图确保该组的更新方法在产品被修改和销毁时被称作示例。 似乎一切都运转良好, 事件被调用, 属性看起来很好, 但当我称之为产品模型中的销毁方法时却失败了。 更新后, 我试图打印产品收集的内容, 产品被删除前的产品被删除。 如果我在500米超时后调用这个调试线, 内容是正常的。 产品被删除等 。

所以根据我的理解 产品销毁事件被叫来 然后在从收藏中实际移除之前 传到团体

最佳回答

Backbone通过收听关于模型的 dstroy 事件来清除收藏中被摧毁的模型:见Backbone.Model - 销毁 和 < a href="http://docuscloud.github.com/backbone/docs/backbone.html#section-105" rel="nofolpol">Backbone.collection -onModelEvent 。

操作器执行的顺序没有保障, 您必须使用其它东西 。 例如, 在收藏中收听 < code> destroy event, 这将在 < strong > 后点火 。 模式实际上被删除 :

initialize: function(data){
    var _this = this;

    var products = new Products(data.Product);
    this.set( Product , products);

    this.get( Product ).on("destroy", this.update, this);
    this.bind( change , this.update, this);
},

请选中此小提琴 < a href=> http://jsfiddle. net/NUtmt/" rel="nofollow" > http://jsfiddle. net/Nutmt/ 完整示例 。

问题回答

暂无回答




相关问题
What s the appropriate granularity for Backbone.js Views?

I m adopting Backbone.js to render a small corner of an existing large web app. If this goes well, I can see Backbone.js growing to encompass the whole of the app, lending some much-needed structure ...

Rendering Layouts with Backbone.js

If you were to build a single page web application (SPWA) using Backbone.js and jQuery with--for example--two controllers that each required a unique page layouts, how would you render the layout? ...

Load html without refresh

Im unsure of how to approach this, should I have the html to be loaded hidden or load it from somewhere? I want to load a form in one page, and dynamic content on other pages. The form can be saved ...

Why are my CoffeeScript/backbone.js events not firing?

I m trying to familiarize myself with CoffeeScript and backbone.js, and I must be missing something. This CoffeeScript: MyView = Backbone.View.extend events: { "click" : "testHandler" ...

热门标签