0

バックボーンに次のモード/コレクション/ビューのセットアップがあります。

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 のコレクションからモデルを取得しようとすると、sortableCID によってモデルがログに記録されません。次の出力が得られます。

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が、まだ undefined が返されます... 各モデルには独自の cid があります。どんな助けでも大歓迎です。

4

3 に答える 3

0
  • 引数としてモデル ID がcid必要なため、渡されるself.collection.get(cid)のはいずれかのモデルの ID であることを確認してください。この目的のためにcollection.get、値もログに記録することをお勧めします。cid
于 2013-03-06T11:51:15.197 に答える