我的模型设置有问题



我有以下路线:

this.resource('categories', function() {
    this.route('add');
    this.resource('category', {path: ':category_id'}, function() {
        this.route('edit', {path: "/edit"});
        this.resource('products', function(){
            this.route('add');
            this.resource('product', {path: ':product_id'});
        });
    });
});

除了编辑路线外,其他一切都正常。出于某种奇怪的原因,我的模型没有初始化。

App.CategoryRoute = Ember.Route.extend({
    model: function (params) {
        console.log(this.store.getById('category', params.category_id)); // Returns what I want
        return this.store.getById('category', params.category_id); 
    }, 
    //other stuff
});

经过几次尝试/错误后,我发现我的CategoryEditRoute覆盖了我的模型。我不是100%确定这个声明,但它看起来像。如果我试图在这个路线上设置我的模型,我无法访问我的参数。。。没有我的参数,我不知道加载什么模型!!!

App.CategoryEditRoute = Ember.Route.extend({
    model: function (params) {
        console.log(params); // Result in: Object {}
        return this.store.getById('category', params.category_id);  // Undefined
    }, 
    // Other stuff
)}

谢谢你花时间帮忙。

App.CategoryEditRoute = Ember.Route.extend({
    model: function() {
        return this.modelFor('category')    
    }, 
});

这将使用在类别资源中解析的模型。

还要注意,例如,Ember中的路由与Rails中的路由不同。如果视图嵌套,则通常会嵌套管线。这样做没有错(实际上可能会让事情变得越来越简单):

this.route('categoryEdit', {path: ':category_id/edit'})

最新更新