简体   繁体   English

Ember.js:如何将计算属性用作路由

[英]Ember.js: How to use computed property as a route

I'm pretty new to Ember.js and am building an app to pick up some Ember chops. 我是Ember.js的新手,正在构建一个应用程序以获取一些Ember印章。 I wanted to use a computed property in one of my models as a route but it seems something isn't working correctly. 我想在我的一个模型中使用计算属性作为路由,但似乎某些方法无法正常工作。 I'm using FIXTURES by the way. 我正在使用FIXTURES。

What I'm trying to achieve is /peeps/john-smith instead of /peeps/1 我想要实现的是/peeps/john-smith而不是/peeps/1

I've got my model setup like this: 我有这样的模型设置:

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')
});

My router setup is like this: 我的路由器设置是这样的:

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') };
    }
});

When I navigate to /peeps/john-smith in the browser, I get this warning in my console You made a request for a peep with id john-smith . 当我在浏览器中导航到/peeps/john-smith时,我在控制台中收到此警告。 You made a request for a peep with id john-smith

Is there something I'm missing? 有什么我想念的吗?

默认情况下,它会搜索通过id PARAM,所以你可以任意更改适配器,使其通过搜索slug或尝试添加idEmber.computed.alias('slug')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM