2013-10-17 86 views
2

我試圖通過RESTful後端爲我的持久層實現抽象,並遇到了一些我覺得有點困惑的事情。我正在使用角度框架,更具體地說是使用ngResource模塊來訪問$ resource服務。我可以執行我的查詢,並在沒有問題的情況下針對後端工作。我的問題出現在重新集成到我的kendo-ui數據源中時,數據源永遠無法識別查詢何時返回。根據我的理解,$ resource將立即返回一個空集合(數組),以便分配可能,然後在查詢結果完成時使用查詢結果填充該數組。 Kendo-ui的DataSource應該關注這個變量,並在更新時反映給任何利用數據源的人。我已經使用稍微不同的模型成功實現了這一點(傳遞一個對象字面值,我根據需要更新自己),並且DataSource在識別更新時沒有問題。任何見解都會有所幫助!

app.provider('remotePersistence', function() { 
     this.$get = function ($resource) { 
      var definitions = { 
       widgets: $resource('http://127.0.0.1:3000\:3000/widget.json',{},{ 
        archive: { method: 'POST', params: { archive: true }}, 
        active: { method: 'POST', params: { active: true }} 
       }) 
      }; 

      var datastore = {} 
      var namespaces = ['widgets']; 
      namespaces.forEach(function (namespace) { 
       datastore[namespace] = { 
        endpoint: definitions[namespace], 
        cache: definitions[namespace].query() 
       }; 
      }); 
      return datastore; 
     }; 
    }); 

    app.controller(
    "WidgetsSearchController", 
    function ($scope, remotePersistence){ 

     $scope.widgets = undefined; 

     $scope.visibleWidgets = new kendo.data.DataSource({ 
      // data: remotePersistence.widgets.cache, 
      transport: { 
       read: function (options) { 
        options.success(remotePersistence.widgets.cache); 
       } 
      } 
     }); 
    }); 
    //This works but is not desirable style 
    //$scope.widgets = remotePersistence.widgets.query(function(){ $scope.visibleWidgets.data($scope.widgets) }); 
+0

只是一個交叉鏈接 - 類似的問題/問題(http://stackoverflow.com/questions/19289854)。國際海事組織,這是AngularJS/KendoUI集成的一個非常粗糙的優勢。在整個回調/承諾模式中,替換數據在AngularJS中很常見。簡單的'k-datasource'聲明應該足以讓這個工作。 – EBarr

回答

1

對於後面的任何人在這裏是我目前的實施,很好地工作。我仍然對我必須做的操作感到有點不高興,但它與分頁一起工作。

app.controller(
    "WidgetSearchController", 
    function ($scope, remotePersistence){ 

     $scope.visibleWidgets = new kendo.data.DataSource({ 
      widget: { 
       read: function (options) { 
        if(options.data.sort){ 
         options.data.order = _.map(options.data.sort, function (sortItem) { 
          return sortItem.field + " " + sortItem.dir 
         }).join(", "); 
        } 
        remotePersistence.widgets.endpoint.query(options.data, function(response){ 
         console.log(response); 
         options.success(response); 
        }); 
       } 
      }, 
      schema: { 
       data: "widgets", 
       total: "total" 
      }, 
      pageSize: 20, 
      serverSorting: true, 
      serverPaging: true 
      // serverFiltering: true 
     }); 
    }); 

app.provider(
    'remotePersistence', 
    function() { 
     this.$get = function ($resource) { 
      var definitions = { 
       widgets: $resource('http://127.0.0.1:3000\:3000/widgets/:id',{ id: '@id' },{ 
        archive: { method: 'PUT', params: { archive: true }}, 
        update: { method: 'PUT' }, 
        active: { method: 'PUT', params: { active: true }}, 
        query: { method: 'GET', isArray: false}, 


       }) 
      }; 

      var datastore = {} 
      var namespaces = ['widgets']; 
      namespaces.forEach(function (namespace) { 
       datastore[namespace] = { 
        endpoint: definitions[namespace], 
        cache: definitions[namespace].query() 
       }; 
      }); 
      return datastore; 
     }; 
    }); 
1

需要通知數據源已收到數據。 ngResource模塊在完成加載數據時可能會觸發一些回調或事件。然後,您可以使用Kendo DataSource的data()方法用數據項填充它。綁定到該數據源的所有Kendo UI小部件將在您使用數據方法時收到通知。

+0

我想我需要深入研究kendo的dataSource代碼,我的錯誤印象是數據屬性如果一個對象字面值被包裝在一個可觀察值中並且管理它自己的回調。感謝您的澄清。 –