从回调emberjs嵌套组件中访问"this"作为父级

Asa*_*han 5 ember.js dropzone.js

我有一个父组件,其模板包含来自https://www.npmjs.com/ember-cli-dropzonejs的dropzone组件:

{{drop-zone url='#' addRemoveLinks=true success=fileReceived}}
Run Code Online (Sandbox Code Playgroud)

在父控制器中,我有一个调用的方法fileReceived,当成功事件在dropzone上触发时调用该方法.但是,我想在调用方法时调用存储在控制器上的其他方法fileReceived但我无法访问this.我尝试设置所谓的实例变量selfthisdidInsertElement,但它给我的窗口,而不是我的分量.

这是我的父组件控制器:

import Ember from 'ember';

export default Ember.Component.extend({
  self:null,
  didInsertElement:function()
  {
    this.set('self', this);
  },
  fileReceived: function (file) {
    //Validate sheet

    this.sendAction('doStuff', file); //"this" returns a dropzone object instead of parentObject
    //this.doStuff(file);
  },
  actions: {
    doStuff: function (file) {
      //do stuff with the file
  }
});
Run Code Online (Sandbox Code Playgroud)

Ada*_*hts 2

我认为fileReceived应该是在行动之内,然后this.sendAction应该是this.send。那我想这会是你想要的东西吗?

import Ember from 'ember';

export default Ember.Component.extend({
  actions: {
    fileReceived: function (file) {
        //Validate sheet

        this.send('doStuff', file); //"this" returns a dropzone object instead of parentObject
        //this.doStuff(file);
  },
    doStuff: function (file) {
      //do stuff with the file
  }
});
Run Code Online (Sandbox Code Playgroud)

编辑

正如评论中所讨论的,您还需要将模板更改为

{{drop-zone url='#' addRemoveLinks=true success=(action 'fileReceived')}}
Run Code Online (Sandbox Code Playgroud)