我正在使用从服务器中提取的 json 文件来配置我的网站,并告诉每个页面它的标题是什么。json 文件如下所示:
[{"route": "student", "title": "Student Info Page"}, {"route": "payments", "title": "Payments and Pricing"}, {"route": "policy", "title": "Mine"}, {"route": "biography", "title": "About Me"}]
用于使用以下代码创建导航栏:
App.MenuController = Ember.ArrayController.create();
$.get('config.json', function(data) {
App.MenuController.set('content', data);
});
然后在模板中使用:
{{#each App.MenuController}}
{{#varLink route this}}{{title}}{{/varLink}}
{{/each}}
到目前为止,所有这些都很好。
所以这是我的问题:我希望以App.Router.map
编程方式完成路由映射,使用这个 json 对象来确定应该存在哪些路由。
我到底应该怎么做?我搜索了文档,然后尝试了这个:
$.get('config.json', function(data) {
App.MenuController.set('content', data);
for (var i = 0; i < data.length; i++) {
App.Router.map(function(){
var r = data[i].route;
console.log(r);
this.route(r);
});
}
});
它给出了以下控制台读数:
student app.js:9
payments app.js:9
policy app.js:9
biography app.js:9
Assertion failed: The attempt to linkTo route 'student' failed. The router did not find 'student' in its possible routes: 'index' ember.js:361
Uncaught Error: There is no route named student.index ember.js:23900