Bos*_*ohn 2 javascript ember.js
我有一条我已经剥离的余烬路线
App.MyRoute = Ember.Route.extend({
model: function(params){
console.log("model function executing");
Ember.Object.create()
},
setupController: function(controller){
console.log("setupController function executed");
}
});
Run Code Online (Sandbox Code Playgroud)
当我切换到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>
Run Code Online (Sandbox Code Playgroud)
使用应用代码:
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");
}
});
Run Code Online (Sandbox Code Playgroud)
当您单击示例路径的链接时,"在setupController中"会打印,但"在模型函数中"不会.
linkTo使用transitionTo幕后.每当我们使用时transitionTo,我们实际上直接提供上下文/模型,因此model不会调用路径上的方法.在上面的示例中,您有{{#linkTo example App.thing}}.因为我们已经知道上下文App.thing没有理由触发该model方法.model当我们不知道模型是什么时,我们只调用路线.这种情况发生的主要时间是通过URL更改进入.