2

如何将外部值传递给控制器​​。在下面的代码中,我想传入值filtertypefiltertermfrom PostsControllerinto 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(..);
  }
});
4

1 回答 1

1

您不能将 args 传递给控制器​​的 init() 函数。

要将外部值传递给控制器​​,您应该使用绑定。特别是控制器的needs属性。请参阅 ember 指南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')
}

});

于 2013-08-22T07:04:37.483 回答