2

我正在尝试为我的持久层实现一个 RESTful 后端的抽象,并且遇到了一些我觉得有点困惑的事情。我正在使用 Angular 框架,更具体地说是 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) });
4

2 回答 2

1

需要通知数据源已收到数据。也许 ngResource 模块在完成加载数据时会触发一些回调或事件。然后可以使用 Kendo DataSource 的data () 方法来填充数据项。当您使用 data 方法时,绑定到该数据源的所有 Kendo UI 小部件都会收到通知。

于 2013-10-18T07:54:16.917 回答
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;
        };
    });
于 2013-10-20T15:46:48.777 回答