2013-04-08 199 views
0

我感到困惑如何設置從我的(動態)模型Ember.js灰燼,與模型

這裏獲取信息的工作是我的模型(到目前爲止作品):

App.Router.map(function() { 
     this.resource('calendar', { path: '/calendar/:currentMonth'}); 
}); 

App.CalendarRoute = Ember.Route.extend({ 
    model: function (params) { 
    var obj = { 
     daysList: calendar.getDaysInMonth("2013", params.currentMonth), 
     currentMonth: params.currentMonth 
    }; 
    return obj; 
    } 
}); 

我只是想拿回「currentMonth」屬性:

App.CalendarController = Ember.Controller.extend({ 
    next: function() { 
    console.log(this.get('currentMonth')); 
    } 
}); 

但我得到一個「未定義」的錯誤。

我必須顯式聲明我的模型(Ember.model.extend())才能獲取和設置值嗎?

回答

3

有一些conventions,您可能不知道將Model設置爲Controller

Route中,模型可以是您定義的任何對象或對象集合。有很多適用的約定,並且在大多數情況下,您不必指定任何內容,因爲它使用各種對象的名稱來引導自己構建查詢並設置控制器的內容,但是,在您的特定的代碼,您將返回obj作爲模型。

Ember提供了一個名爲setupController的掛鉤,它將此對象設置爲您的控制器的content屬性。例如:

App.CalendarRoute = Ember.Route.extend({ 
    model: function (params) { 
    var obj = { 
     daysList: calendar.getDaysInMonth("2013", params.currentMonth), 
     currentMonth: params.currentMonth 
    }; 
    return obj; 
    }, 
    setupController: function(controller, model) { 
    // model in this case, should be the instance of your "obj" from "model" above 
    controller.set('content', model); 
    } 
}); 

雖這麼說,你應該嘗試console.log(this.get('content.currentMonth'));