2013-12-03 182 views
0

我有以下途徑:有麻煩我的模型建立

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

感謝您抽出時間幫助。

回答

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

這將使用已在類別資源中解析的模型。例如,請注意,Ember中的路由與Rails中的路由不同。如果視圖嵌套,通常會嵌套路由。沒有什麼不妥(並且實際上可能使事情變得更容易和更簡單的)這樣做:

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

+0

非常感謝,我在這個問題像過去2-3個小時。我會盡快接受答案(我必須等待幾分鐘)。 –