2013-05-09 76 views
2

如何從模型中刪除模型的索引。 在下面的代碼中,有一個回調函數doSomething(){},當remove被觸發時被調用,並且我希望索引在那裏。如何從模型中刪除模型的索引 - 骨幹

Backbone doc說「刪除前的模型索引作爲options.index提供給聽衆」, 但我得到的選項是未定義的。

MyModel = Backbone.Model.extend({});  

var MyCollection = Backbone.Collection.extend({ 
    model : MyModel 
}); 

var data = [ 

    { 
     "id": 1, 
     "value": 600 
    }, 
    { 
     "id": 2, 
     "value": 800 
    }, 
    { 
     "id": 3, 
     "value": 700 
    }, 
    { 
     "id": 7, 
     "value": 100 
    } 

]; 

var newCollection = new MyCollection(); 
newCollection.on('remove',doSomething); 
newCollection.reset(data); 

function doSomething(){/* how to get the index of the deleted model */} 
newCollection.remove(newCollection.at(2)); 

console.log(newCollection); 

小提琴 - http://jsfiddle.net/Z7Qmg/

回答