簡體   English   中英

如何使函數從cordova-plugin-file-transfer上傳返回承諾

[英]How to make the function upload from cordova-plugin-file-transfer return a 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 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)"

有沒有更好的方法可以使插件的upload功能與Promise完美配合?

謝謝

您忘記創建延期了。 你只是在做

this.savePromise = $.Deferred;

當你真正想要

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

$.Deferred工廠確實沒有.resolve方法。

順便說一句,您將要return this.savePromise.promise()而不是延遲對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM