2016-11-04 16 views
0

@EmberJS 2.6EmberJS:非同期モデル

は、現在の問題は、呼び出しがアプリを返すために時間がかかりすぎる場合は、基本的に「凍結」されていること、である

import Ember from 'ember'; 

export default Ember.Route.extend({ 

    model: function() { 
     ... 
     var ajax_response = Ember.$.ajax(url, { 
      "type": 'POST', 
      "async": false, 
      "headers": { 
      "Authorization": _auth_header 
      }, 
      "data": JSON.stringify(data), 
      "contentType": "application/json" 
     }); 
     if (ajax_response.hasOwnProperty("responseJSON")) { 
      var response = ajax_response.responseJSON; 

      if (response.hasOwnProperty("status") && response.status === success_status) { 
      response = response.result; 

      if (typeof response === "object") { 
       return response; 
      } 
      } 
     } 
    } 
}); 

これをやってイム。私は、ページがレンダリングされた、1非同期作業にコードのこの部分を変更したい、との約束を移入するために戻ったとき:

{{#each model as |transaction|}} 
.... 

答えて

1

深いつもりはない場合は、あなたのコードはそれほど変更することができます。

import Ember from 'ember'; 

export default Ember.Route.extend({ 

    model: function() { 
    return []; 
    }, 
    setupController: function(controller, model) { 
     controller.set('model',model); 

     //here you can fire loader in template based on isLoading in controller; 
     controller.set('isLoading',true); 

     ... 
     var ajax_response = Ember.$.ajax(url, { 
      "type": 'POST', 
      "async": false, 
      "headers": { 
      "Authorization": _auth_header 
      }, 
      "data": JSON.stringify(data), 
      "contentType": "application/json" 
     }); 
     if (ajax_response.hasOwnProperty("responseJSON")) { 
      var response = ajax_response.responseJSON; 

      if (response.hasOwnProperty("status") && response.status === success_status) { 
      response = response.result; 

      if (typeof response === "object") { 
       controller.set('isLoading',false); 
       controller.get('model').pushObjects(response); 

      } 
      } 
     } 
    } 
});