我正在尝试为我的持久层实现 RESTful 后端的抽象,但遇到了一些让我感到有点困惑的问题。我正在使用 Angular 框架,更具体地说是 ngResource 模块来访问 $resource 服务。我可以毫无问题地执行查询并在后端工作。当集成回我的 kendo-ui 数据源时,我的问题出现了,数据源永远无法识别查询何时返回。根据我的理解, $resource 将立即返回一个空集合(数组)以进行可能的分配,然后在完成时用查询结果填充该数组。 Kendo-ui 的数据源应该监视此变量,并在更新时将其反射(reflect)给利用该数据源的任何人。我已经使用稍微不同的模型(传递我根据需要自己更新的对象文字)成功实现了这一点,并且数据源在识别更新时没有问题。任何见解都会有帮助!
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) });
最佳答案
需要通知数据源数据已收到。也许 ngResource 模块在完成加载数据时会触发一些回调或事件。然后您可以使用data Kendo DataSource 的 () 方法用数据项填充它。当您使用该数据方法时,绑定(bind)到该数据源的所有 Kendo UI 小部件都将收到通知。
关于angularjs - Kendo-UI 数据存储利用 Angular $resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19430085/