2014-01-29 64 views
0

我目前正在學習骨幹,並試圖包裝我的頭爲什麼需要初始化函數視圖和集合?初始化集合中的屬性

下面是我的一些代碼:

Tasks = Backbone.Collection.extend({ 
    //This is our Task collection and holds our Task models 
    initialize: function (models, options) { 
     console.log(options); 

     this.bind("add", options.view.addTaskListeners); 
     console.log(this.bind("add", options.view.addTaskListeners)); 
     //Listen for new additions to the collection. 
    } 
    }); 




    //master view 


    AppView = Backbone.View.extend({ 
    el: $("body"), 
    initialize: function() { 
     this.tasks = new Tasks(null, { view: this }); 
     // Create a task collection when the view is initialized. 
     // Pass it a reference to this view to create a connection between the two 
    }, 
    events: { 
     "click #add-task": "showPrompt", 
    }, 

本教程的創建者決定利用這個「初始化」屬性,但初始化從未用作屬性,如Tasks.initialize或AppView.initialize。我嘗試更改屬性的名稱,但它不起作用。這是一個保留字還是關鍵字?爲什麼需要?謝謝!

回答

1

初始化是一個特殊的功能(即「保留」)。如果它是在視圖/模型/集合上定義的,它將在視圖/模型/集合即時生效時執行(即,當您致電new時)。所以如果你改變它的名字,它將不會被Backbone自動調用...