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