【问题标题】:Having trouble getting my model set up无法设置我的模型
【发布时间】:2013-12-20 00:23:10
【问题描述】:

我有以下路线:

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

感谢您抽出宝贵时间提供帮助。

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:
    App.CategoryEditRoute = Ember.Route.extend({
        model: function() {
            return this.modelFor('category')    
        }, 
    });
    

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

    请注意,Ember 中的路由与 Rails 中的路由不同,例如。如果视图是嵌套的,您通常会嵌套路由。这样做没有任何问题(实际上可能会让事情变得更容易和更简单):

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

    【讨论】:

    • 非常感谢,在过去的 2-3 个小时里,我一直在解决这个问题。我会尽快接受答案(我必须等待几分钟)。
    猜你喜欢
    • 2014-02-21
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2018-01-24
    相关资源
    最近更新 更多