簡體   English   中英

無法通過CID在Backbone集合中獲取模型

[英]Unable to get a model in Backbone collection by CID

我在骨干網中有以下模式/集合/視圖設置。

var Card = {};  

//card model
Card.Model = Backbone.Model.extend();


//collection
Card.Collection = Backbone.Collection.extend({
    url: '/api/cards',
    model: Card.Model
});

//column view
Card.MainView = Backbone.View.extend({
        className: 'column',
        append: function(model) {
            var view = Card.View.extend({ 
                model : model,
                attributes: { cid : model.cid } 
            });
            var v = new view();
            this.$el.append(v.el);          
        },
        initialize: function() {
            var self = this;
            _.bindAll(this, "sortable");
            this.collection.fetch({ 
                success: function() { 
                    self.render(); 
                } 
            });
        },
        render: function() {
            var self = this;
            for(var i = 0; i < this.columns.length; i++) {
                var col = this.columns[i];
                var column = Card.ColumnView.extend({
                    data: col,
                    cards: self.collection.where({ position : col.position })
                });
                var col = new column();
                this.$el.append(col.el);
            }               
            this.sortable();
        },
        sortable: function() {
            var self = this;
            $(this.el).find('.card-container').sortable({
                connectWith: '.card-container',
                receive: _.bind(function(event, ui) { 
                    var cid = $(ui.item).attr('cid');
                    var card = self.collection.get(cid);
                    //console.log(self.collection.last());
                    console.log('cid', self.collection.get(cid));
                    console.log('index 1', self.collection.at(1));
                    console.log('last', self.collection.last());
                    console.log('collection', self.collection);
                }, this)

            });
        }
}); 

當我嘗試從MainView的sortable函數集合中獲取模型時,它不會通過CID記錄模型。 我得到以下輸出。

cid undefined CardMetaCollection.js:97
index 1 d {collection: d, attributes: Object, _escapedAttributes: Object, cid: "c2", changed: Object…} CardMetaCollection.js:98
last d {collection: d, attributes: Object, _escapedAttributes: Object, cid: "c249", changed: Object…} CardMetaCollection.js:99
collection d {length: 249, models: Array[249], _byId: Object, _byCid: Object, url: "/api/cards"…}

我已經嘗試為fetch方法登錄success回調,但是仍然返回未定義的返回值……每個模型都有自己的cid。 任何幫助深表感謝。

.get()方法接受idcid id可以直接傳遞給get ,但是cid必須作為對象文字傳遞。 實際上,您也可以在對象文字中傳遞id ,並使代碼一致。

var cid = ...;
var card = self.collection.get({ cid: cid });

var id = ...;
var card = self.collection.get({ id: id });

var id = ...;
var card = self.collection.get(id);

您可以看一下骨架.get()中的.get()實現。

更新 :可以單獨將cid傳遞給Backbone 0.9.9或更高版本中的.get()方法(請參閱當前1.3.3中實現 )。

var cid = ...;
var card = self.collection.get(cid);

在你的情況,問題很可能在屬性“CID”丟失卡上的元素,如寫道。 您聲明了.append()方法,將該屬性添加到新元素中,但是在.render()方法中調用了new column()而不是它。 看來, column視圖沒有添加該屬性,因為您的日志記錄發現該屬性undefined

注意:例如,考慮使用有效的自定義HTML屬性名稱,例如“ data-cid”,而不僅僅是“ cid”。

  • 確保cid要傳遞到self.collection.get(cid)是一個ID為模型中的任何一個,因為collection.get預計模型ID作為參數。 為此,您可能還想記錄cid值。

當您將“差異模型”推到一個集合中而其余部分不響應唯一請求的唯一請求時,可能會使用cid,或者REST不會返回ID attr,或者我們想創建沒有休息的假模型並將其推入集合中

 var banned = app.request('rest:banList');
  var whiteIp = app.request("rest:inviteList");
   var whiteGroup = app.request("rest:groupList");
            $.when(banned, whiteIp, whiteGroup).done(function (bannedList,  whiteIpList, whiteGroupList) {
                debugger
                var arr = new Backbone.Collection();
                arr.add(bannedList);
                arr.add(whiteIpList);
                arr.add(whiteGroupList);

            });

define(["app"], function (app) {
    var API = {
        getWho: function (data) {
            var whois = new Backbone.Model();
            whois.url = '/admin/whois';
            whois.cid = 'ban';
            var defer = $.Deferred();

            whois.fetch({
                type: 'GET',
                data: data,
                success: function(data){
                    debugger
                    defer.resolve(data);
                },
                error: function(data){
                    defer.reject(data);
                }
            });
            return defer.promise();
        }
    };

    app.reqres.setHandler("rest:getWho", function (data) {
        return API.getWho(data);
    });
});

暫無
暫無

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

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