2013-06-26 3 views
1

Допустим, у меня есть следующие настройки: ОтложенныйКак цепь JQuery Deferreds

var dfr = new Deferred() 

dfr.done(step1) 
.then(step2) 
.then(step3) 

есть способ передать результат step2 в step3.

ответ

1

Да.

Предполагая step1, step2 и step3 быть JavaScript функции, то просто получить step2, чтобы вернуть результат, и он автоматически становится параметром передается step3.

1

Да, вы можете передать результат .lets увидеть пример загрузки файлов, так что это будет ясно для вас ..

fun5().then(
      function (msg) { 
      if(msg==='m1'){ 
      var dfd1 = $.Deferred(function (dfd1){ 
      //alert("called2"); 

      var remoteFile = "http://www.freegreatdesign.com/files/images/6/2921-large-apple-icon-png-3.jpg"; 
      var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1); 
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { 
      fileSystem.root.getFile(localFileName, {create: true, exclusive: false}, function(fileEntry) { 
      var localPath = fileEntry.fullPath; 
      if (device.platform === "Android" && localPath.indexOf("file://") === 0) { 
      localPath = localPath.substring(7); 
      } 

      var ft = new FileTransfer(); 
      ft.download(remoteFile, localPath, function(entry) { 
      dfd1.resolve('m2'); 
      // Do what you want with successful file downloaded and then 
      // call the method again to get the next file 
      //downloadFile(); 
      }, fail); 
      }, fail); 
      }, fail); 

      //alert("In 2"); 
      }); 

      } 
      return dfd1.promise(); 
      }).then(
      function (msg) { 
      if(msg==='m2'){ 
      var dfd2 = $.Deferred(function (dfd2){ 
      //alert("called3"); 

      var remoteFile = "http://www.freegreatdesign.com/files/images/6/2921-large-apple-icon-png-3.jpg"; 
      var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/')+1); 
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { 
      fileSystem.root.getFile(localFileName, {create: true, exclusive: false}, function(fileEntry) { 
      var localPath = fileEntry.fullPath; 
      if (device.platform === "Android" && localPath.indexOf("file://") === 0) { 
      localPath = localPath.substring(7); 
      } 

      var ft = new FileTransfer(); 
      ft.download(remoteFile, localPath, function(entry) { 
      dfd2.resolve('m3'); 
      // Do what you want with successful file downloaded and then 
      // call the method again to get the next file 
      //downloadFile(); 
      }, fail); 
      }, fail); 
      }, fail); 

      //alert("In 3"); 
      }); 

      } 
      return dfd2.promise(); 
      })