2012-11-16 31 views
2

我正在學習Backbone,我正試圖弄清楚我正在使用哪個函數庫的'on'函數。我認爲這是jQuery,但如果是這樣,我不理解API。有人可以解釋'開'功能或將我鏈接到一些文檔。第一個參數是事件。第二個參數是被調用的函數。最後的'這個'是指什麼(我假設調用類),爲什麼它需要?這裏是我的代碼直接從阿迪·奧斯馬尼,這是APPVIEW:Backbone.js on(),最後這是指什麼?

initialize : function() { 
     this.input = this.$('#new-todo'); 
     this.allCheckbox = this.$('#toggle-all')[0]; 
     this.$footer = this.$('#footer'); 
     this.$main = this.$('#main'); 

     window.app.Todos.on('add', this.addOne, this); 
     window.app.Todos.on('reset', this.addAll, this); 
     window.app.Todos.on('change:completed', this.filterOne, this); 
     window.app.Todos.on("filter", this.filterAll, this); 

     window.app.Todos.on('all', this.render, this); 

     app.Todos.fetch(); 
    }, 
+0

,如果你指的是window.app.Todos.on(「所有」 ,this.render,this); - 它會觸發Todos系列的任何變化。 (添加,重置,更改等) – nxtwrld

回答

1

的在這種情況下,方法是從骨幹的事件模塊到來。它接受三個參數 - 事件名稱,函數和上下文。上下文決定'this'的值應該在函數內部。

Todos.on("filter", this.filterAll, this);你剛剛問的是函數內部filterAll的「本」的價值應該是你的看法實例

0
object.on(event, callback, [context]) 

按照backbone.js doc最後[背景]參數是可選的背景下,這將是傳遞給回調函數。

在阿迪的待辦事項例如,是傳遞一個參考待辦事項視圖被點擊:

// Add a single todo item to the list by creating a view for it, and 
// appending its element to the `<ul>`. 
addOne: function(todo) { 
    var view = new app.TodoView({ model: todo }); 
    $('#todo-list').append(view.render().el); 
},