我有一个应用程序,当遵循link-to
一切按预期工作时,但是当直接导航到子路由时,我的模型数据似乎没有加载。在我的孩子(类型)路线中,我this.modelFor
在模型钩子中使用。这是一个 bin http://emberjs.jsbin.com/oSApiSeh/7#/types/secondary直接导航到那里不会显示颜色,但是如果单击辅助它可以工作。
这是该jsbin的来源:
// ... groupBy definition omitted
App = Ember.Application.create();
App.Router.map(function() {
this.resource('colors', { path: '/' }, function() {
this.resource('types', { path: 'types/:type_group' }, function() {});
});
});
App.ColorsRoute = Ember.Route.extend({
model: function() {
return App.Color.find();
}
});
App.TypesRoute = Ember.Route.extend({
model: function(params) {
return this.modelFor('colors').filterBy('type', params.type_group);
}
});
App.ColorsController = Ember.ArrayController.extend({
grouped: _groupBy('type')
});
App.TypesController = Ember.ArrayController.extend({});
App.Color = Ember.Model.extend({
'color':Ember.attr('string'),
'type': Ember.attr('string')
});
App.Color.adapter = Ember.FixtureAdapter.create();
App.Color.FIXTURES = [
{ color:'red', type: 'primary'},
{ color:'green', type: 'primary'},
{ color: 'yellow', type: 'secondary'},
{ color: 'orange', type: 'secondary'},
{ color: 'blue', type: 'primary'}
];
我的模板:
<script type="text/x-handlebars">
<h2> Welcome to Ember.js</h2>
{{outlet}}
I am wanting a route to .../primary that has red, green, and blue as its model and a route to .../secondary that has yellow and orange in its model
</script>
<script type="text/x-handlebars" data-template-name="colors">
<ul>
{{#each grouped}}
<li>{{#link-to 'types' group}}{{group}} ({{content.length}}){{/link-to}}</li>
{{/each}}
</ul>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="colors/index">
all colors
</script>
<script type="text/x-handlebars" data-template-name="types">
Types
<ul>
{{#each item in controller}}
<li>{{item.color}} </li>
{{/each}}
</ul>
</script>