我有一条我已经精简到的 ember 路线
App.MyRoute = Ember.Route.extend({
model: function(params){
console.log("model function executing");
Ember.Object.create()
},
setupController: function(controller){
console.log("setupController function executed");
}
});
当我切换到 MyRoute 时,setupController 会被执行,但填充模型的函数永远不会执行。模型最终只是在标签msg
中传递的对象。 {{link myRoute msg}}
在我们切换到该路线时,我需要加载/计算模型的某些部分。为此,我需要能够成功更新模型,或者我需要从 setupController 函数中访问链接中传递的参数。关于如何最好地实现这一目标的建议?
编辑
为了尝试解决这个问题,我创建了一个完整的最小示例来产生这种行为:
我的html是:
<html>
<head>
<title> This is my Page! </title>
<script src="js/libs/jquery-1.8.2.js"></script>
<script src="js/libs/handlebars-1.0.rc.1.js"></script>
<script src="js/libs/ember.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<script type="text/x-handlebars">
{{#linkTo example App.thing}}<p> go </p>{{/linkTo}}
<div>
{{outlet}}
</div>
</script>
<script type="text/x-handlebars" data-template-name="index">
<p> Initial Text </p>
</script>
<script type="text/x-handlebars" data-template-name="example">
<p> After change </p>
</script>
</body>
</html>
使用应用程序代码:
var App = Ember.Application.create();
App.Router.map(function() {
this.resource("example", {path: "/example/:id"});
});
App.thing = Ember.Object.create({
id:10,
});
App.ExampleRoute = Ember.Route.extend({
model: function(params){
console.log("in model function");
return new Ember.Object.create();
},
setupController: function(controller){
console.log("in setupController");
}
});
当您单击示例路由的链接时,会打印“in setupController”,但不会打印“in model function”。