【问题标题】:How to preform a component action rather than a route action in ember-file-upload如何在 ember-file-upload 中执行组件操作而不是路由操作
【发布时间】:2017-11-07 12:19:18
【问题描述】:

我正在使用这个包 https://github.com/tim-evans/ember-file-upload

在他们的示例中,他们调用路由操作来上传图片

             onfileadd=(route-action "uploadImage")}}

但是我想改为调用组件操作

我试过了

                 onfileadd="uploadImage"}}

并在组件中添加动作:

uploadImage: function (file) {
  console.log(file);
  let self = this;
  RSVP.cast(Ember.$.get(ENV.APP.API_HOST + "/v1/teams/signed_url/")).then(function (response) {
    return file.upload(response.url, {
      data: response.credentials
    });
  }).then(function (response) {
    self.set('url', response.headers.Location);
  });
},

但我得到了错误:

未捕获的类型错误:无法读取未定义的属性“uploadImage”

如果我将其移动到路由,该操作将按预期工作,但我需要更新组件中的正确属性(路由中不存在)

知道我可以做些什么来将其更改为组件功能吗?

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    您应该使用action 助手:

    onfileadd=(action "uploadImage")}}
    

    还要确保 uploadImage 在您的组件中的 actions 哈希内:

    export default Ember.Component.extend({
      // ...
      actions: {
        uploadImage() { // <-- make sure this is inside the actions hash
          // ...
        }
      }
    })
    

    【讨论】:

    • 对不起,我没有分享整个代码,它在动作哈希注释中添加动作助手工作!
    • @MohamadZein 很高兴能帮助队友:)
    猜你喜欢
    • 2017-09-05
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多