Ada*_*sky 2 ember.js ember-router
这是小提琴.
http://jsfiddle.net/inconduit/hf7XM/10/
重现问题的步骤:
似乎返回的控制器引用controllerFor()实际上并不是PostsIndexController?这是为什么?
我在路径的setupController()钩子中攻击它以在App上设置对该控制器的全局引用,当我将该引用的内容设置为新数组时emptyList2(),列表在模板中正确清除.
我用controllerFor()错了吗?还是误解了它的回归?这是一个范围问题吗?请帮帮我.
App.PostsIndexRoute = Ember.Route.extend({
setupController : function(controller,model) {
controller.set('content',['one','two','three']);
App.postsIndexController = controller;
}
});
// receives the {{action}} from the template
App.PostsController = Ember.Controller.extend({
emptyList : function() {
this.controllerFor('postsIndex').set('content',Ember.A());
},
emptyList2 : function() {
App.postsIndexController.set('content',Ember.A());
}
});
Run Code Online (Sandbox Code Playgroud)
tldr:替换controllerFor('postsIndex')为controllerFor('posts.index')
emptyList2 fx正在运行,因为你将App.postsIndexController常量设置为传递给控制器实例的任何东西setupController.
我认为比如何解决这个问题更重要的是如何调试这些问题.这是我做的:
在运行JS小提琴时打开JS控制台.点击帖子链接但在尝试清空列表之前,我运行了以下内容:
Em.keys(App.__container__.cache.dict)
["application:main", "router:main", "route:application", "route:index", "controller:application", "template:application", "controller:index", "template:index", "route:posts", "route:posts.index", "controller:posts", "template:posts", "controller:posts.index", "template:posts.index"]
Run Code Online (Sandbox Code Playgroud)
然后在单击emptyList操作后,再次尝试:
Em.keys(App.__container__.cache.dict)
["application:main", "router:main", "route:application", "route:index", "controller:application", "template:application", "controller:index", "template:index", "route:posts", "route:posts.index", "controller:posts", "template:posts", "controller:posts.index", "template:posts.index", "controller:postsIndex"]
Run Code Online (Sandbox Code Playgroud)
看看现在有2个App.PostsIndexController的缓存实例:controller:postsIndex和controller:posts.index
然后我向jsFiddle添加了一些console.log来查看被引用的实例.从那里很容易做出修复.由于Ember为每个对象添加了一个toString()方法,因此很容易看到发生了什么.例如:
controllerFor("postsIndex").toString()
<App.PostsIndexController:ember218>
Run Code Online (Sandbox Code Playgroud)
更新了小提琴:http://jsfiddle.net/mgrassotti/Aa2WX/2/
| 归档时间: |
|
| 查看次数: |
4320 次 |
| 最近记录: |