0

XHR返回功能我有灰燼的服務通常提供AJAX後獲得燼阿賈克斯服務

import Ember from 'ember'; 
import ENV from '../config/environment'; 

export default Ember.Service.extend({ 
    save(data) { 
    return new Ember.RSVP.Promise(
     function (resolve, reject) { 
     function progress(e){ 

      if(e.lengthComputable){ 
      let max = e.total; 
      let current = e.loaded; 
      let Percentage = (current * 100)/max; 
      console.log(Percentage); 
      if(Percentage >= 100) 
      { 
       // process completed 
      } 
      } 
     } 

     Ember.$.ajax({ 
      type: 'POST', 
      enctype: 'multipart/form-data', 
      url: ENV.APP.API_HOST + '/profile/lampiran-desas', 
      processData: false, 
      contentType: false, 
      cache: false, 
      data: data, 
      xhr: function() { 
      let myXhr = $.ajaxSettings.xhr(); 
      if(myXhr.upload){ 
       myXhr.upload.addEventListener('progress',progress, false); 
      } 
      return myXhr; 
      }, 
      success: function (response) { 
      resolve(response); 
      }, 
      error: function (error) { 
      reject(error); 
      } 
     }); 
     }); 
    } 
}); 

成功響應回在我的組件。但是如何讓我的功能進步迴歸。所以我可以在我的模板上加載進度條..?

感謝..

+0

您需要手動設置加載狀態。裝載將僅處理路線的模型掛鉤。有關詳細信息,請參閱此twiddle https://ember-twiddle.com/#/53bd472bbeaa42d1790da2ba97a6a803?openFiles=templates.components.my-comp.hbs%2Ctemplates.components.my-comp.hbs其中,我將使用'isLoading'明確設置加載! –

回答

0

據我看,你這是在服務的遠程調用,您希望顯示組件中的進展。爲此,您需要將相關數據保存在服務中;並顯示組件中的進度。

這是twiddle我已經準備好爲你說明情況。 mock-remote-call-service是一個模擬服務,每200毫秒更新一次進程10%。結果直接保存到服務本身。 progress-displayer-component有責任顯示進度,啓動遠程呼叫任務,甚至顯示返回的數據。

這只是一個模擬的例子來解釋我的觀點。我希望這有幫助。