2013-08-20 44 views
0

我有一個意外的行爲,也許你可以讓我明白那裏存在的問題。我認爲這是一個功能,但我無法理解它。ember.js:控制器的意外行爲

路線:

App.LacesRoute = Ember.Route.extend 
    model: -> App.Lace.find() 

App.LaceRoute = Ember.Route.extend 
    model: (params) -> 
    App.Lace.find(params.lace_id) 
    setupController: (controller, model)-> 
    controller.set('content', model) 

控制器:

App.LacesController = Ember.ArrayController.extend 
    contentCount: (
    -> @get("content").toArray().length 
).property("content") 

列表模板:

{{contentCount}} 
{{#each controller}} 
{{this}} 
{{/each}} 

詳細信息模板這裏ininfluent

路由器:

@resource "laces", -> 
    @resource "lace", {path: ":lace_id"} 

當我訪問/laces計數打印0,但所有的鞋帶在each

上市當我訪問/laces/1計數打印出正確的數量和鞋帶正確列出

回答

2

在你前面的代碼你正在返回

@get("content").toArray().length 

這是不需要的,因爲這內容是DS.RecordArray,它的行爲就像一個數組,並且具有length財產。

所以此工程:

@get("content.length") 

但我認爲,主要問題是property("content"),你必須指定事項的計算性能,在這種情況下的價值,並不是全部內容,但你的財產length。因此正確的是property("content.length")

最終的結果是:

App.LacesController = Ember.ArrayController.extend 
    contentCount: (
    -> @get("content.length") 
).property("content.length") 
+0

它的工作原理。請問你或任何人解釋我爲什麼? –

+0

@VecchiaSpugna我用更多的信息更新了答案。我希望它有幫助。 –

+0

謝謝。不幸的是,我認爲改變內容也會影響其長度 –

0

每當你重寫「模式」,並在一個航「setupController」,那麼,你需要調用超在「setupController」再次,這樣的事情:

App.LaceRoute = Ember.Route.extend 
    model: (params) -> 
    App.Lace.find(params.lace_id) 
    setupController: (controller, model)-> 
    this._super(controller, model) 
    controller.set('content', model) 
+0

無變化 –