2015-01-15 35 views
0

我想輪詢一個後端,使用Restangular,直到滿足條件。我正在使用Angular Poller進行投票(https://github.com/emmaguo/angular-poller)。它返回一個承諾。角度輪詢與Restangular

以下代碼正確執行輪詢,但無法訪問返回的數據。在然後中的console.log未打印。

這裏發生了什麼?

var myPoller = poller.get(Restangular.one('batches',$routeParams.panel_id), { 
    action: 'get', 
    delay: 1000, 
    arguementsArray: [] 
    }); 


    myPoller.promise.then(function(batch){ 
    $scope.running = batch.batch_status; 
    console.log('Status: ' + batch.batch_status); 
    if (batch.batch_status === 'complete'){ 
     myPoller.stop(); 
    } 
    }); 

UPDATE

對角輪詢的文檔是指回調。

https://github.com/emmaguo/angular-poller#customize-restangular-poller

myPoller.promise.then(null, null, callback); 
+0

你的空,空值在哪裏? –

+0

我不太清楚回調應該如何工作。我可以創建一個名爲「callback」的函數,console.logs可以從那裏工作,但是如何訪問返回的數據? – ardochhigh

+0

你正確地指出,thge文檔說'.... then(null,null,callback)',但是你自己的代碼是'.... then(callback)'。那麼你的空,空值在哪裏? –

回答

0

我的工作了。對於任何想要使用Angular Poller使用Restangular的人,這裏是我的工作代碼。

我被卡住的部分是如何從Restangular訪問返回的對象。原來你只是假設它正在被傳遞,並在回調中引用它。

然後可以通過查看返回的數據來建立輪詢停止條件。

var callback = function(batch) { 
    console.log("in the callback " + batch.batch_status); 
    $scope.running = batch.batch_status; 
    if (batch.batch_status.hasOwnProperty('complete')){ 
     myPoller.stop(); 
    } 

    } 


    var myPoller = poller.get(Restangular.one('batches',$routeParams.panel_id), { 
    action: 'get', 
    delay: 1000, 
    arguementsArray: [] 
    }); 

    myPoller.promise.then(null,null,callback); 
+0

我仍然不知道null是什麼,null表示代碼正常工作... – ardochhigh