如何将外部值传递给控制器。在下面的代码中,我想将filtertype
中的值filterterm
和PostsController
传递给PostsDynamicController
。有什么方法可以做到这一点?
我有这样的模板
<script type="text/x-handlebars" id="posts">
{{view Ember.Select
contentBinding="App.names.content"
valueBinding="App.names.selected"
}}
{{view Ember.TextField valueBinding="filterterm" }}
<button {{action "submit"}} > Submit</button>
{{outlet}}
</script>
我的部分App.js是这样的:
App.PostsController = Ember.ObjectController.extend({
content: [],
filterterm: "",
submit: function () {
var filtertype = App.names.selected;
var filterterm = this.get('filterterm');
this.transitionToRoute("posts.dynamicfinder");
}
});
App.PostsDynamicController = Ember.ObjectController.extend({
init: function () {
//want access to filtertype and filterterm here so that I can pass them in find. i.e.
//App.Request.find(filtertype: filterterm);
this.set('model', App.Request.find(..);
}
});
答案 0 :(得分:1)
您无法将args传递给控制器的init()函数。
要将外部值传递到控制器,您应该使用绑定。特别是控制器的needs
属性。请参阅余烬指南dependencies-between-controllers
例如:
// Change handlebars template to valueBinding="filtertype" instead of valueBinding="App.names.selected"
// Also these should be ArrayControllers not ObjectControllers
App.PostsController = Ember.ArrayController.extend({
filterterm: null,
filtertype: null,
submit: function () {
this.transitionToRoute("posts.dynamicfinder");
}
});
App.PostsDynamicController = Ember.ArrayController.extend({
needs: ['posts'],
termBinding: 'controllers.posts.filterterm',
typeBinding: 'controllers.posts.filtertype',
filteredPosts: function() {
var filtertype = this.get('type');
var filterterm = this.get('term');
// ...
}.property('term', 'type')
}
});