简体   繁体   English

如何使函数从cordova-plugin-file-transfer上传返回承诺

[英]How to make the function upload from cordova-plugin-file-transfer return a promise

I'm using the cordova-plugin-file-transfer plugin for cordova with a backbone based application. 我正在将Cordova-plugin-file-transfer插件用于带有主干应用程序的cordova

I have a Backbone.View that has a method called saveReport . 我有一个Backbone.View ,它具有一个名为saveReport的方法。 Then I have a Backbone.Model that has a function called savePic . 然后,我有一个Backbone.Model ,它具有一个名为savePic的函数。 This is how myView.saveReport looks like: 这是myView.saveReport样子:

saveReport:function(){
    var promise = $.Deferred();
    promise.resolve();
    var that = this;
    promise.then(function(){
      var savePicPromise = $.Deferred();
      savePicPromise.resolve();
      for(var i=1; i< that.miniatureViews.length; ++i){
        savePicPromise = savePicPromise.then(function(){
          return that.miniatureViews[i].pictureModel.savePic();
        });
      }
      return savePicPromise;
    }).then(function(){
       // here I do all other things
       // ...
       // ...
    }); // promise chain. 

myModel.savePic looks like this: myModel.savePic看起来像这样:

savePic: function(){
      var url = this.urlRoot;
      var options = new FileUploadOptions();
      options.fileKey="image";
      options.fileName=this.imgURI.substr(this.imgURI.lastIndexOf('/')+1);
      options.mimeType="image/jpeg";

      var ft = new FileTransfer();
      var myResponse; // to be set by callbacks.

      var that = this;
      this.savePromise = $.Deferred; // this should put a promise object into the model itself. 

      ft.upload(this.imgURI,
        encodeURI(url),
        function(r){
          that.savedURL = r.response; // this is the URL that the APi responds to us.
          that.savePromise.resolve();
        },
        function(e){
          console.error(e);
          window.analytics.trackEvent('ERROR', 'savingPic',e.code);
          that.savePromise.fail();
        },
        options);

      return this.savePromise;
    },

I also made some changes in the code and also tried this configuration with 2 other model's methods: 我还对代码进行了一些更改,还尝试了其他2种模型方法的配置:

ft.upload(this.imgURI,
        encodeURI(url),
        this.resolveSavePromise,
        this.failedSavePromise,
        options);

with the 2 functions: 具有2个功能:

resolveSavePromise: function(r){
      this.savedURL = r.response; // this is the URL that the APi responds to us.
      this.savePromise.resolve();
    },
    failedSavePromise: function(e){
      console.error(e);
      window.analytics.trackEvent('ERROR', 'savingPic',e.code);
      this.savePromise.fail();
    },

Note: in this second option, I don't return anything within the savePic method. 注意:在第二个选项中,我不会在savePic方法中返回任何内容。

The problem is that in the for loop in the saveReport method the promise stored in the pictureModel isn't actually a promise, or at least behaves strange. 问题是在saveReport方法的for循环中,存储在pictureModel中的promise实际上不是promise,或者至少表现得很奇怪。 I get an error on the line that says this.savePromise.resolve() : that.savePromise.resolve is not a function. (In 'that.savePromise.resolve()', 'that.savePromise.resolve' is undefined)" 我在说this.savePromise.resolve()的一行上收到错误: that.savePromise.resolve is not a function. (In 'that.savePromise.resolve()', 'that.savePromise.resolve' is undefined)" that.savePromise.resolve is not a function. (In 'that.savePromise.resolve()', 'that.savePromise.resolve' is undefined)"

Is there a better way to make the upload function of the plugin work nicely with promises? 有没有更好的方法可以使插件的upload功能与Promise完美配合?

thanks 谢谢

You forgot to create the deferred. 您忘记创建延期了。 You were just doing 你只是在做

this.savePromise = $.Deferred;

while you actually wanted 当你真正想要

this.savePromise = new $.Deferred; // or
this.savePromise = new $.Deferred(); // or
this.savePromise = $.Deferred();

The $.Deferred factory does indeed not have a .resolve method. $.Deferred工厂确实没有.resolve方法。

Btw, you'll want to return this.savePromise.promise() instead of the deferred object. 顺便说一句,您将要return this.savePromise.promise()而不是延迟对象。

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

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