-1

我正在尝试实现路由器事件并使用路由器的发送功能来触发路由器上的事件。但无法获得有关此的任何文档。
我想要实现的是我正在从控制器/视图中引发一个事件以从服务器获取数据。并且事件从服务器异步获取数据,当成功获取数据时,我想从调用事件的位置初始化视图的子视图,即我需要知道何时获取数据。但我认为路由器上的事件不会返回任何让我知道通话何时结束的信息。
类似的东西:
查看:

Em.View.extend({
   click: function(){
       var recordsPromise = this.get('controller.target').send('getRecords');
       recordsPromise.done(this.initiateProtocol);
   },

   showChild: false,
   initiateProtocol: function(){
       //this showChild is used as a condition in the template to show/hide
       // the childview. And is being set after the call completed 
       // successfully
       //here childOneView is present some other js file and is fetched using requirejs
       require(['childOneView'], $.proxy(function(childOneView){
           this.set('childOne', childOneView.extend({
               templateName: 'childOne'
           });
           this.set('showChild', true); 
       }, this));

   }
}

路由器

Em.Route.extend({
    events: {
        getRecords: function(){
            //make an ajax call to fetch the records and return the ajax as a
            // promise
        }
    }
});

模板

{{#if view.showChild}}
    {{view view.childOne}}
{{/if}}
4

1 回答 1

0

我认为惯用的 Ember 方法会有所不同。将动作发送到控制器并让它冒泡到路由,然后设置视图将通过绑定响应的属性:

看法

App.RecordsView = Em.View.extend(Ember.ViewTargetActionSupport, {
  click: function(){
    this.triggerAction({action: 'getRecords'})
  }
});

控制器

App.RecordsController = Em.ArrayController.extend({
  content: [],
  isLoaded: false
});

模板

<!-- records.handlebars -->
{{#if isLoaded}}
  render stuff here... perhaps {{#each this}}{{someRecordProperty}}{{/each}}
{{/if}}

路由器

App.RecordsRoute = Em.Route.extend({
  events: {
    getRecords: function(){
      var controller = this.controllerFor('records');
      $.ajax(...).then(function(data){
        Em.run(function(){
          controller.setProperties({
            content: data.foo,
            isLoaded: true
          });
        });
      });
    }
  }
});
于 2013-05-03T14:32:09.490 回答