1

I have an app that displays a list of results based on the route (specially, query parameters with an Emberjs plugin). These results come from the server's response and require a find() call on the model. I'm trying to use an Ember.select view's dropdown to build the query parameters to filter the results, but I can't seem to find a way to do this, because:

I'm not sure how to (or if I even can) create events from a controller. Currently, I have a self-created controller (with createWithMixins) that receives the selected binding from the Ember.select view:

App.SlotsFiltersController = Ember.Object.createWithMixins({
  weekday: null,
  kind: null,
  neighborhood: null,

  filtersChanged: function() {
    // I'd like to send an event to my current route here
  }.observes('weekday', 'kind', 'neighborhood')
});

Which properly observes the changes from the Ember.Select, but doesn't have access to the router.

I've also tried subclassing the Ember.Select view, and sending an event on valueDidChange. This does reach the route, however the value has not yet been updated in the controller (I would be filtering with stale data).

Finally, I've tried extending the controller that is instantiated by the route; I had no success with this (observer events didn't seem to fire and I wasn't sure how to debug).

Is there a good way to do this? I feel like I'm just going in the wrong direction and after searching forever I just haven't found anything similar that is still up-to-date (post non-global App.Router).

4

1 回答 1

1

我不确定如何(或者我什至可以)从控制器创建事件。目前,我有一个自己创建的控制器(使用 createWithMixins)...

好的,这就是问题所在。以这种方式创建控制器绝不是一个好主意,而是应该由 ember 创建和管理控制器。当 ember 创建控制器时,它将注入控制器所需的属性,例如指向路由的“目标”属性。然后你可以像这样向路由发送事件:

App.SlotsFiltersController = Ember.Controller.extend({
  weekday: null,
  kind: null,
  neighborhood: null,

  filtersChanged: function() {
    target.send('eventName')
  }.observes('weekday', 'kind', 'neighborhood')
});

App.SlotsFiltersView = Ember.SelectView.extend({})

现在从您的模板中,使用{{render}}帮助程序在 SlotsFiltrersController 的上下文中呈现 SlotsFiltersView。

{{render slotsFilters}}
于 2013-07-21T02:07:00.703 回答