2013-02-25 2 views
17

컬렉션에서 모델을 제거하고 제거 이벤트를 실행하려면 어떻게합니까? 나는 people.remove([{ name: "joe3" }]);을 시도했지만 작동하지 않을 것이다. 이렇게함으로써컬렉션에서 모델 제거 및 화재 제거 이벤트 - backbone.js

var Person = Backbone.Model.extend({ 

    initialize: function() { 
     console.log(" person is initialized"); 
    }, 
    defaults: { 
     name: "underfined", 
     age:"underfined" 
    } 
}); 

var People = Backbone.Collection.extend({ 
    initialize: function() { 
     console.log("people collection is initialized"); 
     this.bind('add', this.onModelAdded, this); 
     this.bind('remove', this.onModelRemoved, this); 
    }, 
    model: Person, 
    onModelAdded: function(model, collection, options) { 
     console.log("options = ", options); 
     alert("added"); 
    }, 
    onModelRemoved: function (model, collection, options) { 
     console.log("options = ", options); 
     alert("removed"); 
    }, 
}); 

//var person = new Person({ name: "joe1" }); 
var people = new People(); 



//people.add([{ name: "joe2" }]); 
people.add([{ name: "joe1" }]); 
people.add([{ name: "joe2" }]); 
people.add([{ name: "joe3" }]); 
people.add([{ name: "joe4" }]); 
people.add([{ name: "joe5" }]); 

people.remove([{ name: "joe3" }]); 



console.log(people.toJSON()); 

답변

30

:

people.remove([{ name: "joe3" }]); 

당신이 people 컬렉션에 연결되어 있지 않은 그냥 일반 개체를 전달할 때문에, 모델을 제거하지 마십시오.

people.remove(people.at(2)); 

또는 :

var model = new Person({name: "joe3"}); 
people.add(model); 
... 
people.remove(model); 

뿐만 아니라 작동 대신 당신이 뭔가를 할 수 있습니다.

그래서 컬렉션에서 실제 모델 객체를 참조해야합니다. 당신이 collection.where 전화와 함께 체인 간단하게 할 수하는 제거 찾고 다른 사람들을위한

+0

thnaks - 하나 개 더 질문을하시기 바랍니다 - 나는 모델의 속성의 값을 얻는 방법 제거 된 함수 : onModelRemoved : function (model, collection, options) { console.log ("options =", options); 경고 ("removed"+ model.attributes.name + ""+ model.attributes.age + "has been removed"); } –

+0

model.attributes.age가 작동하지 않습니다. –

+3

model.get ('age') –

5
var Person = Backbone.Model.extend({ 
    defaults: { 
     name: "underfined", 
     age:"underfined" 
    } 
}); 

var People = Backbone.Collection.extend({ 
    initialize: function() { 
     this.bind('remove', this.onModelRemoved, this); 
    }, 
    model: Person, 
    onModelRemoved: function (model, collection, options) { 
     alert("removed"); 
    }, 
    getByName: function(name){ 
     return this.filter(function(val) { 
      return val.get("name") === name; 
     }) 
    } 
}); 

var people = new People(); 

people.add(new Person({name:"joe1"})); 
people.add(new Person({name:"joe2"})); 
people.remove(people.getByName("joe1")); 

console.info(people.toJSON()); 
+0

그렇다면 removeByName() – Tosh

+0

이 맞습니다. 그것은 빠른 예제 –

34

http://jsfiddle.net/kD9Xu/

. 같은 검색과 일치하는 모든 항목을 제거합니다 :

people.remove(people.where({name: "joe3"})); 

는 또 다른 방법은 조금 짧은 Backbone collection.where

+3

이모, 가장 좋은 대답 – walv

+0

네 이것은 내가 찾고 있던 것 같았다. 간단하고 직선적 인 대답입니다. – mcraen

4

를 참조하고 뿐만 아니라 컬렉션에 대한 이벤트를 제거 화재 :

people.at(2).destroy(); 
// OR 
people.where({name: "joe2"})[0].destroy(); 

모델에서 "destroy"이벤트를 트리거합니다.이 이벤트는 해당 모델을 포함하는 모든 콜렉션에서 거품을 일으 킵니다. . "[0]"다음 코드를 사용하여 제거하기 위해 http://backbonejs.org/#Model-destroy

0

:

people.findWhere({name: "joe2"}).destroy();