Backbonejs:在视图中呈现但在集合中不存在的模型

时间:2012-08-22 23:48:59

标签: javascript backbone.js

我只是不知道导致问题的原因并需要帮助。在发布之前,我已经找到了替代解决方案,但我想了解为什么这种方法无法正常工作。

我有路由器初始化视图,初始化实体集合和视图,如下所示:

advertiser_manage_campaign: function () {
    this.campaignListView = new window.CampaignListView;
    this.mainSidebar = new window.MainSidebar;
},

CampaignListView:

window.CampaignListView = Backbone.View.extend({

    el: ("#right_column"),

    initialize: function () {   
        this.render();

        this.campaignCollection = new Campaign.CampaignCollection;
        this.campaignCollectionView = new Campaign.CampaignCollectionView({ model: this.campaignCollection });
        this.campaignCollection.fetch();
    },

    events: {
        "click .campaign_dialog": "openCampaignDialog"
    },

    openCampaignDialog: function (e) {        
        var that = this;
        var itemID = $(e.target).attr("item-id");
        var model = {}; //model to populate dialog inputs
        if (!isNaN(itemID))
            model = this.campaignCollection.get(itemID).toJSON(); //get existing model from collection <- after described procedure, error

        Campaign.Dialog.open(model, function (data) {
            if (isNaN(itemID)) {//model does not exist, create
            that.campaignCollection.create(data, { wait: true,
                    error: function (model, error) {
                        dialoger.showErrors(JSON.parse(error.responseText).errors);
                    },
                    success: function (mdl, response) { window.Campaign.Dialog.close(); }
                });               

            } else {//model exist, update
                model = that.campaignCollection.get(itemID);
                model.save(data, { wait: true,
                    error: function (mdl, error) {
                        dialoger.showErrors(JSON.parse(error.responseText).errors);
                    },
                    success: function (mdl, response) { window.Campaign.Dialog.close(); }
                });
            }
        });
        return false;
    },

    render: function () { 
        $(this.el).html(window.Templates.getHTML("campaign_list_view", {}));
        $(".button", $(this.el)).button();
    }
});

-

openCampaignDialog 

适用于编辑模型和创建新模型。模型的每个视图(表格行) 具有类“.campaign_dialog”的按钮,并且有用于添加具有相同类的新模型的按钮。

Campaign.Dialog.open

显示使用model填充的对话框,并在回调中从对话框窗体返回JSON。

如果我通过对话框创建新模型,我可以立即编辑它,但是当我创建新模型时,更改视图,返回到此视图,再次创建新模型,更改视图然后再返回,单击上次添加的编辑item,我在注释行上得到错误作为模型,此ID不在集合中,尽管它是。服务器响应正常。显然,我做错了什么,过了一天,我看不出它是什么。

我遇到的替代解决方案是从模型视图事件创建和填充对话框(这可行),但我认为CampaingCollectionView或CampaingView不应该处理添加或编辑模型,所以我在''中实现了这个更高的观点。

感谢大家帮助我...

编辑:

var CampaignCollectionView = Backbone.View.extend({

    el: (".content_table tbody"),

    initialize: function () {
        this.model.bind("reset", this.render, this);
        this.model.bind("add", this.add, this);
    },

    render: function () {
        $(this.el).empty();
        _.each(this.model.models, function (campaign) {
            $(this.el).append(new CampaignView({ model: campaign }).render().el);
        }, this);
        return this;
    },

    add: function (model) {
        window.Appender.AppendAndScroll($(new CampaignView({ model: model }).render().el), this.el);
    }

});

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

  

当我们通过这些将对象绑定在一起时,会出现问题   事件,但我们不打扰解除他们。只要这些物体   绑定在一起,我们的应用程序代码中有一个参考   其中至少有一个,他们不会被清理或垃圾收集。该   导致内存泄漏就像电影的僵尸 - 藏在里面   黑暗的角落,等着跳出去吃午饭。

来源:http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

作者建议解除绑定机制,但如果存在,我将重用相同的对象。

路由器:

advertiser_manage_campaign: function () {
    if (!this.campaignListView)
        this.campaignListView = new window.CampaignListView;
    else
        this.campaignListView.initialize();


    this.mainSidebar = new window.MainSidebar;
},

如果有人认为这不是最佳解决方案,我想听听原因。

感谢所有试图提供帮助的人!