簡體   English   中英

Backbonejs:模型在視圖中呈現,但在集合中不存在

[英]Backbonejs: model rendered in view but dont exist in collection

我只是不知道是什么導致了問題並需要幫助。 發布之前,我已經提出了替代解決方案,但是我想了解為什么它不能正常工作。

我有初始化視圖的路由器,該視圖初始化實體集合和視圖,如下所示:

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

顯示用模型填充的對話框,並在回調中從對話框形式返回JSON。

如果我通過對話框創建新模型,則可以立即對其進行編輯,但是當我創建新模型時,請更改視圖,回到該視圖,再次創建新模型,更改視圖,然后再次返回,單擊對最后添加的項目的編輯,在注釋行上獲取錯誤,因為具有此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);
    }

});

我找到了解決方案。

但是,當我們通過這些事件將對象綁定在一起但不打擾它們的綁定時,就會出現問題。 只要這些對象綁定在一起,並且我們的應用程序代碼中至少有其中一個引用,它們就不會被清理或垃圾回收。 導致的內存泄漏就像電影中的僵屍一樣-藏在黑暗的角落里,等着跳出來吃我們的午餐。

資料來源: 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;
},

如果有人認為這不是最佳解決方案,我想聽聽為什么。

謝謝所有嘗試提供幫助的人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM