2015-10-06 29 views
0

我想顯示一個項目列表的'顯示'模板。Ember嵌套的路徑show.hbs params不起作用

我index.hbs的工作原理如下:

路線/ index.js

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model: function() { 
    return this.store.find('item'); 
    } 
}); 

模板/項目/ index.hbs

items/index.hbs 
<ul> 
    {{#each model as |item|}} 
     <li> 
     {{#link-to 'items.show' item}} 
      {{item.name}} 
     {{/link-to}} 
     </li> 
    {{else}} 
     <li>No contacts found.</li> 
    {{/each}} 
</ul> 

然而,當我點擊一個鏈接,它會將我帶到正確的路線(localhost:4200/items/1),但是,它會給我出現以下錯誤:Error while processing route: items.show Assertion Failed: You may not pass 'undefined' as id to the store's find method Error: Assertion Failed: You may not pass 'undefined' as id to the store's find method

這是我show.js和HBS:

路線/ show.js

import Ember from 'ember'; 
export default Ember.Route.extend({ 
    model: function(params) { 
    return this.store.findAll('item', params.id); 
    } 
}); 

和模板/項目/ show.hbs

{{name}} 

這裏是router.js

import Ember from 'ember'; 
import config from './config/environment'; 

var Router = Ember.Router.extend({ 
    location: config.locationType 
}); 

export default Router.map(function() { 
    this.resource('items', function() { 
    this.route('show', {path: ':item_id'}); 
    }); 
}); 

我無法弄清楚它不工作的時候!我讀了參數不是從索引發送到顯示..但然後?!

預先感謝您。任何誇大的答案將非常感激。

+0

請問您的router.js是否使用item_id指定show route?這將有助於如果你顯示你的router.js – xoma

+0

@xoma是的,我添加了上面的參考 –

回答

2

希望這有助於。 http://ember-twiddle.com/147af82f6fa69bf97414

你的代碼片段密切關注後,我意識到,您的項目中:模型鉤您傳遞params.id到store.findAll return this.store.findAll('item', params.id),但是在你指定它作爲你的ITEM_ID router.js。您應該使用路徑定義中使用的相同參數名稱。