[英]Ember.js: How to use computed property as a route
我是Ember.js的新手,正在構建一個應用程序以獲取一些Ember印章。 我想在我的一個模型中使用計算屬性作為路由,但似乎某些方法無法正常工作。 我正在使用FIXTURES。
我想要實現的是/peeps/john-smith
而不是/peeps/1
我有這樣的模型設置:
App.Peep = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
slug: function(){
this.get('firstName').toLowerCase() + '-' + this.get('lastName').toLowerCase();
}.property('firstName', 'lastName')
});
我的路由器設置是這樣的:
App.Router.map(function(){
this.resource('peep', { path: '/peeps/:peep_slug'});
});
App.PeepRoute = Ember.Route.extend({
model: function(params){
return this.store.find('peep', params.peep_slug);
},
serialize: function(model){
return { peep_slug: model.get('slug') };
}
});
當我在瀏覽器中導航到/peeps/john-smith
時,我在控制台中收到此警告。 You made a request for a peep with id john-smith
。
有什么我想念的嗎?
默認情況下,它會搜索通過id
PARAM,所以你可以任意更改適配器,使其通過搜索slug
或嘗試添加id
為Ember.computed.alias('slug')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.