2013-03-13 55 views
0

我遇到了一些奇怪的行爲,從路由訪問動態段。無法訪問Ember.js中的動態段對象路由

我有一個關鍵字模型。在顯示關鍵字集合時,我希望路徑爲/keywords。訪問單個關鍵字時,我想要路徑爲/keyword/:KEYWORD_ID。按照慣例,灰燼要你做下面...

this.resource('keywords', { path: '/keywords' }, function() { 

    this.route('new', { path: '/new' }); 
    this.resource('keyword', { path: '/keyword/:keyword_id' }, function() { 

     this.route('edit'); 

    }); 

}); 

爲了實現上述行爲,我做了以下...

this.resource('keywords', { path: '/keywords' }, function() { 

    this.route('new', { path: '/new' }); 

}); 

this.resource('keyword', { path: '/keyword/:keyword_id' }, function() { 

    this.route('edit'); 

}); 

然而,使用第二種方法時, KeywordIndex的路由(即單個關鍵字對象),params對象爲空,並且屏幕上的內容爲空。

App.KeywordIndexRoute = Ember.Route.extend({ 

    model: function(params) { 

      return App.Keyword.find(params.keyword_id); 

    }, 

    renderTemplate: function(controller, model) { 

     this.render({outlet: 'page'}); 

    } 

}); 

有沒有人遇到過這個問題?有沒有更好的方法來解決這個問題?

+0

作爲後續,我在這裏使用了方法描述器(http://stackoverflow.com/questions/14609757/emberjs-1-0-0pre4-how-do-you-pass-a-context-object-to-a-resource-index-狂勝)。但是,這適用於所有情況,除了有些通過URL直接訪問「關鍵字詳細信息」視圖(即在瀏覽器中鍵入../keyword/ID並點擊輸入)。在這種情況下,屏幕顯示第二個數據,然後消失。在查看模型時,您可以看到所有數據都被擦除了對象(ID)。奇怪。 – 2013-03-13 16:12:55

+0

您是否知道當通過URL輸入路線時,模型掛鉤纔會執行?很難說,問題是什麼,因爲你不會談論你如何在你的路線之間轉換。 – mavilein 2013-03-13 19:35:58

回答

0

問題已解決。這個問題出現在我們針對這個特定對象的API中。最好的方法,對於那些考慮這個路由模式如下...

this.resource('keywords', { path: '/keywords' }, function() { 

    this.route('new', { path: '/new' }); 

}); 

this.resource('keyword', { path: '/keyword/:keyword_id' }, function() { 

    this.route('edit'); 

}); 

你會再訪問你的控制器這樣的模型......

App.KeywordIndexRoute = Ember.Route.extend({ 

    model: function(params) { 

     return this.modelFor("keyword"); 

    }, 

    renderTemplate: function(controller, model) { 

     this.render({outlet: 'page'}); 

    } 

});