2013-07-10 53 views
1

在設置應用路由器,我請求一些數據到後端:觸發功能,一旦一個REST請求已完成

App.Node = DS.Model.extend({ 
    ... 
}); 

App.ApplicationRoute = Ember.Route.extend({ 
    processReply: function () { do some processing here; }, 
    setupController: function (controller, model) { 
     this.cache = App.Node.find(); 
    } 
}); 

現在,當從後臺收到的find的結果,我想通過調用this.processReply()

我怎樣才能聽到find請求的完成?我試圖插入.then方法(假設find返回承諾),但是這阻止了我的應用程序。

setupController: function (controller, model) { 
     var _this = this; 
     this.cache = App.Node.find().then(function(data) { 
      _this.process(); 
     }); 
    } 
+0

你在哪裏,刪除了我的答案,因爲它不適用於你的用例 – intuitivepixel

回答

1

setupController是同步的,不像model掛鉤。您可以延遲從承諾中設置控制器的內容。因此,數據加載後,控制器的綁定/計算屬性會觸發。

setupController: function(controller, model) { 
    App.Node.find().then(function(data) { 
    controller.set('content', data); 
    // optionally 
    // controller.process() 
    }); 
} 
+0

謝謝Darshan,但我不明白。 「find」的作用不是設置控制器的內容嗎?對應於'App.Node'的控制器不一定是我在'setupController'中的控制器。在我原來的實現中,我不**設置控制器的內容。爲什麼我應該在聽'then'的時候被迫去做呢?我希望能夠鉤住promise的'.then',而不會對ember在內部處理promise的方式造成任何副作用,最終必須從處理'then'時返回一個值(promise?) '。 – dangonfast

+0

'Route.model()'是使用'find'加載數據的鉤子。當'find'返回一個promise時,路由器會這樣做。在調用setupController時,假定你已經加載了模型,因此第二個參數是'model'。這正是默認的setupController所做的,'controller.set('content',model)'。但是如果在setupController中加載額外的數據,就路由器而言已經太晚了,它的轉換生命週期已經完成。因此你必須手動完成這項工作。 –

相關問題