javascript - 如何使从 cordova-plugin-file-transfer 上传的函数返回一个 promise

标签 javascript jquery cordova backbone.js promise

我正在使用cordova-plugin-file-transfer具有基于主干的应用程序的 cordova 插件。

我有一个 Backbone.View,它有一个名为 saveReport 的方法。然后我有一个 Backbone.Model,它有一个名为 savePic 的函数。这是 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 看起来像这样:

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;
    },

我还在代码中做了一些更改,并使用其他 2 个模型的方法尝试了此配置:

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

有 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();
    },

注意:在第二个选项中,我不会在 savePic 方法中返回任何内容。

问题在于,在 saveReport 方法的 for 循环中,存储在 pictureModel 中的 Promise 实际上并不是一个 Promise,或者至少表现得很奇怪。我收到一条错误消息: this.savePromise.resolve(): that.savePromise.resolve 不是一个函数。 (在 'that.savePromise.resolve()' 中,'that.savePromise.resolve' 未定义)”

有没有更好的方法让插件的upload功能与promise很好地配合?

谢谢

最佳答案

您忘记创建延迟的。你刚刚在做

this.savePromise = $.Deferred;

当你真正想要的时候

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

$.Deferred 工厂确实没有 .resolve 方法。

顺便说一句,您需要返回 this.savePromise.promise() 而不是延迟对象。

关于javascript - 如何使从 cordova-plugin-file-transfer 上传的函数返回一个 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33643463/

相关文章:

javascript - 如何将网页上的位置搜索与 Google map 集成?

php - 将 php 值传递给 javascript

javascript - 使用querySelectorAll过滤具有特定值的选项标签

jquery - phonegap 和 jquery mobile 的 $.getScript 和 css 问题

ios - 将打包的 Atom Electron OSX 应用程序转换为 IOS

javascript - Object.push 似乎不起作用

javascript - 如何在 Angular 4 中使用备用运算符将​​对象推到特定长度

jquery - Bootstrap 的数据表下拉菜单不起作用

jQuery 不显眼的验证忽略 MVC3 中的 data-val-required 消息

android - Cordova:与 native 插件共享 webview cookie