2015-01-05 16 views
0

我第一次使用Angular和Hottowel,並且我想擴展數據服務以利用restangular的功能將REST數據提供給其他hottowel應用程序。擴展HotTowel Dataservice以使用Restangular來使用REST的問題

我已經包括restangular並讓它成功獲得REST。

我意識到,restangular文檔討論在控制器中直接使用promise,所以我現在想知道是否將它包含在數據服務中是可行的。

有問題的功能是getJobs。 它是使用restangular的唯一函數。 它在dataservice中定義爲一個函數,並添加到dataservice變量中以公開它。 它在儀表板控制器中具有匹配的功能。 它也添加到儀表板控制器中的promise的列表中。 我已經確認該函數返回一個在restangular函數中的對象列表。 即使承諾執行then子句,我也不會在儀表板控制器中獲得任何信息。

感謝任何指導。

感謝

西蒙

下面

代碼: -

dataservice.js

(function() { 
'use strict'; 

angular 
    .module('app.core') 
    .factory('dataservice',dataservice); 

dataservice.$inject = ['$q','Restangular']; 
/* @ngInject */ 
function dataservice($q,Restangular) { 
    var service = { 
     getPeople: getPeople, 
     getMessageCount: getMessageCountXXX, 
     getJobs: getJobs 
    }; 

    return service; 



    function getJobs() { 
     var jobs = []; 
     var baseJobs = Restangular.all('jobs'); 

     baseJobs.getList().then(function(jobs) { 
      console.log("Within dataservice.getJobs"); 
      console.log(jobs); 

     }); 
     return $q.when(jobs); 
     console.log("EndofgetJobspost return shouldnt get here");  
    } 


    function getMessageCountXXX() { return $q.when(72); } 

    function getPeople() { 
     var people = [ 
      {firstName: 'John', lastName: 'Papa', age: 25, location: 'Florida'}, 
      {firstName: 'Ward', lastName: 'Bell', age: 31, location: 'California'}, 
      {firstName: 'Colleen', lastName: 'Jones', age: 21, location: 'New York'}, 
      {firstName: 'Madelyn', lastName: 'Green', age: 18, location: 'North Dakota'}, 
      {firstName: 'Ella', lastName: 'Jobs', age: 18, location: 'South Dakota'}, 
      {firstName: 'Landon', lastName: 'Gates', age: 11, location: 'South Carolina'}, 
      {firstName: 'Haley', lastName: 'Guthrie', age: 35, location: 'Wyoming'} 
     ]; 
     return $q.when(people); 
    } 
} 
})(); 

dashboard.Controller.js

dashboard.html的

相關部分: -

<div class="row"> 
      <div class="col-md-6"> 
       <div class="widget wviolet"> 
        <div ht-widget-header title="Jobs" 
         allow-collapse="true"></div> 
        <div class="widget-content text-center text-info"> 
         <table class="table table-condensed table-striped"> 
          <thead> 
           <tr> 
            <th>Name</th> 
            <th>Description</th> 
            <th>Status</th> 
            <th>Category</th> 
           </tr> 
          </thead> 
          <tbody> 
           <tr ng-repeat="p in vm.jobs"> 
            <td>{{p.name}}</td> 
            <td>{{p.description}}</td> 
            <td>{{p.status}}</td> 
            <td>{{p.category}}</td> 
           </tr> 
          </tbody> 
         </table> 
        </div> 
        <div class="widget-foot"> 
         <div class="clearfix"></div> 
        </div> 
       </div> 
      </div>  

回答

1

的問題是,在你的DataService getJobs()函數,你不返回從AJAX調用來的工作清單。用下面的代碼更改該功能。

function getJobs() { 

     var baseJobs = Restangular.all('jobs'); 

     return baseJobs.getList().then(function(jobs) { 
      console.log("Within dataservice.getJobs"); 
      console.log(jobs); 
      return jobs; 

     }); 
    } 
+0

我是按照相同的模式返回到promise結構中,控制器是epxecting --- return $ q.when(jobs); –

+0

如果我將getJobs函數替換爲僅返回基本JSON數組的函數,那麼它的工作正常。 –

+1

實際上,您必須返回即將進行「回調」的數據。當你編寫$ q.when(jobs)時,它會返回[]作爲數據,因爲「then」回調將在某個時間之後執行,並且在你使用$ q.when(jobs)返回[]之前執行。所以,只要嘗試從回調中返回作業,就像我在答案中所示。 –