简体   繁体   English

Ember.js事件控制器触发事件

[英]Ember.js Evented Controller triggering an event

I am trying to trigger an event from an Ember.js controller so the listening views can update themselves. 我正在尝试从Ember.js控制器触发事件​​,以便侦听视图可以自我更新。 Here is the Coffeescript. 这是Coffeescript。

window.CMS.EdmController = Ember.ObjectController.extend Ember.Evented,
  actions:
   save_edm: ->
     postData = $('#edm_form').serialize()
     $.ajax(
       url: 'cms2/update_edm',
       type: 'POST',
       data: postData
       )
       .done ->
         console.log(@)
         @trigger('saveEdmSuccessful')

It fails with an error Object #Object has no method trigger. 它失败,并显示错误消息#Object没有方法触发器。 The @ in the .done function refers to the jQuery post object and not the controller. .done函数中的@引用jQuery post对象而不是控制器。

How do I get the reference to the parent controller object from inside a jQuery callback? 如何从jQuery回调内部获取对父控制器对象的引用?

Any guidance would be appreciated. 任何指导将不胜感激。

I did try to compile your Coffeescript and ended up getting the following code as output: 我确实尝试编译您的Coffeescript并最终得到以下代码作为输出:

window.CMS.EdmController = Ember.ObjectController.extend(Ember.Evented, {
  actions: {
    save_edm: function() {
      var postData;
      postData = $('#edm_form').serialize();
      return $.ajax({
        url: 'cms2/update_edm',
        type: 'POST',
        data: postData
      }).done(function() {
        console.log(this);
        return this.trigger('saveEdmSuccessful');
      });
    }
  }
});

And the solution to the problem you are facing is not related to ember.js, but the way Javascript closures work. 您所面临的问题的解决方案与ember.js无关,而与Javascript闭包的工作方式有关。 A solution to the above problem would be: 上述问题的解决方案是:

window.CMS.EdmController = Ember.ObjectController.extend(Ember.Evented, {
  actions: {
    save_edm: function() {
      var postData;
      postData = $('#edm_form').serialize();
      return $.ajax({
        url: 'cms2/update_edm',
        type: 'POST',
        data: postData
      }).done($.proxy(function() {
        console.log(this);
        return this.trigger('saveEdmSuccessful');
      },this));
    }
  }
});

Notice how I've replaced your .done handler from a function call to a proxy call. 请注意,我是如何将.done处理程序从函数调用替换为代理调用的。 This makes sure that when the callback is executed, the context is updated to reflect the current this . 这样可以确保在执行回调时,上下文会更新为反映当前的this

To understand further about the behaviour of this in JavaScript, read this article . 为了进一步了解有关的行为, this在JavaScript, 请阅读这篇文章

For your reference, here is the CoffeeScript equivalent of my solution: 供您参考,这是我的解决方案的CoffeeScript等效项:

window.CMS.EdmController = Ember.ObjectController.extend Ember.Evented,
  actions:
   save_edm: ->
     postData = $('#edm_form').serialize()
     $.ajax(
       url: 'cms2/update_edm',
       type: 'POST',
       data: postData
       )
       .done(
          $.proxy ->
            console.log(@)
            @trigger('saveEdmSuccessful')
          @
       )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM