2012-07-22 4 views
1

특정 속성이 변경되었을 때 백본 모델이 맞춤 이벤트를 시작하는 좋은 방법은 무엇입니까?백본 - 특정 속성이 변경 될 때 이벤트를 발생시키는 모델

var model = Backbone.Model.extend({ 
    initialize: function(){ 
     // Bind the mode's "change" event to a custom function on itself called "customChanged" 
     this.on('change', this.customChanged); 
    }, 
    // Custom function that fires when the "change" event fires 
    customChanged: function(){ 
     // Fire this custom event if the specific attribute has been changed 
     if(this.hasChanged("a_specific_attribute") ){ 
      this.trigger("change_the_specific_attribute"); 
     } 
    } 
}) 

감사 :

는 지금까지 내가있어 최고입니다!

답변

2

이미 변경 이벤트 고유의 속성을 바인딩 할 수 있습니다 : 변경 한 각 속성에 대해 트리거됩니다 :

var model = Backbone.Model.extend({ 
    initialize: function() { 
    this.on("change:foo", this.onFooChanged); 
    }, 

    onFooChanged: function() { 
    // "foo" property has changed. 
    } 
}); 
+0

아, 훌륭함. 감사! –

1

백본은 이미 이벤트 "속성 변경"을 가지고있다.

var bill = new Backbone.Model({ 
     name: "Bill Smith" 
    }); 

    bill.on("change:name", function(model, name) { 
     alert("Changed name to " + name); 
    }); 

    bill.set({name : "Bill Jones"}); 
관련 문제