在Ember.js中,如果您有一個具有一對多關係的模型,可以根據父模型的屬性生成鏈接?Ember.js:如何在url路徑中使用父模型的屬性?
例如,我有一個模型,像這樣一些數據:
var SuggestionGroup = Ember.Model.extend({
id: Ember.attr('number'),
name: Ember.attr('string'),
suggestions: Ember.hasMany('App.Suggestion', {
key: 'suggestions',
embedded: true
})
});
SuggestionGroup.adapter = Ember.FixtureAdapter.create();
SuggestionGroup.FIXTURES = [
{
id: 1,
name: 'Start',
suggestions: [
{
id: 1,
title: 'Fetching coffee and cake for the Interface Developers',
description: 'Because they\'re so brilliant',
upVotes: 10,
downVotes: 2
},
{
id: 2,
title: 'Criticising the app devs more',
description: 'They enjoy a bit of banter',
upVotes: 8,
downVotes: 4
},
{
id: 3,
title: 'Not updating JIRA',
description: 'It\'ll be funny when the PMs start raging',
upVotes: 1000000,
downVotes: 0
}
]
}]
這裏是我的路線:
App.Router.map(function() {
this.resource('current-suggestions');
this.resource('suggestion', {
path: '/current-suggestions/:suggestion_id'
});
});
我的模板是這樣的:
{{#each model}}
<td>
<ul>
{{#each suggestions}}
<li class="suggestion">
<h3>
{{#linkTo 'suggestion' this}}{{title}}{{/linkTo}}
</h3>
</li>
{{/each}}
</ul>
</td>
{{/each}}
所以,目前我的路徑是current-suggestions/1
。但我想要做的是有路徑,如current-suggestions/Start/1
。所以,路徑是使用父模型的屬性生成的。
是否有可能做到這樣的事情?
爲什麼不在孩子中放置一個'belongsTo'關係?然後,您可以使用get()獲得父對象的引用,然後獲取所需的任何屬性。 – GJK
@GJK它不訪問我遇到問題的父項屬性。我想使用父母的'name'屬性爲每個'Suggestion'創建一個路徑。所以如果一個'Suggestion'的父母的名字是'開始',那麼路徑將是'current-suggestions/Start/1',或者如果是'Stop',那麼'current-suggestions/Stop/1'。 –
啊,我明白了。我想象這樣做的方式是使用動態細分市場,但是我還不夠完善Ember.js,但還不知道如何去做。 – GJK