3

目前我的网站在我的 index.ejs 文件中包含一个搜索栏,当用户按下回车键或单击我的搜索按钮时,它使用 {{action createSearch}} 调用我的控制器的“createSearch”函数。这完美!现在我正在测试新功能,并想直接从我的 index.ejs 文件中调用我的 newSearch 函数,而无需任何用户交互。我尝试在加载时使用 jquery 来模拟按键/单击,但控制器似乎没有调用动作事件。

编辑:包含功能 index.js 代码

         {{view Ember.TextField id="new-search" placeholder="enter a few keywords"
             valueBinding="newSearch" action="createSearch"}}         
        <div id="gobutton" {{action createSearch}}><div id="gobutton_inner">SEARCH</div></div>
4

1 回答 1

3

For system-level actions like this you are better off using events on the ApplicationRoute. Your controllers/views can send an event like createSearch that the route handles typically via the controller that knows how to do this.

App.ApplicationRoute = Em.Route.extend({
  events: {
    createSearch: function(query) {
      var controller = this.controllerFor('search');
      controller.doSearch(query);
    }
  }
});

From any controller's action handler you can trigger the event with,

this.send('createSearch', query);

Or from a view,

this.get('controller').send('createSearch', query);

You will need to wire your individual action handlers to do this event triggering.

于 2013-07-13T04:57:12.937 回答