dgrid JsonRest商店无法正常工作

时间:2014-09-04 15:17:05

标签: asynchronous dgrid

我有以下内容:

require([
    "dojo/dom",
    "dojo/on",
    "dojo/store/Observable",
    "dojo/store/JsonRest",
    "dojo/store/Memory",
    "dgrid/OnDemandGrid"
], function (dom, on, Observable, JsonRest, Memory, OnDemandGrid) {

    var store = new JsonRest({
        target: 'client/list',
        idProperty: 'id'
    });

    var grid = new OnDemandGrid({
        columns: {
            "id": "ID",
            "number": "Name",
            "description": "Description"
        },
        sort: "lastName",
        store: store
    }, "grid");
});

client / list是一个返回json对象{data:[...]}的rest url,但是列表的内容永远不会出现:/

我认为问题是由异步数据加载引起的,因为使用json硬编码对象时内容会显示

编辑:

我通过使用dojo / request成功实现了这一点,但JsonRest通常不应该以相同的方式行事吗?有人能指出我正确的方向吗?

require([
    'dojo/dom',
    'dojo/on',
    'dojo/store/Memory',
    'dojo/request',
    'dgrid/OnDemandGrid'
], function (dom, on, Memory, request, OnDemandGrid) {

    request('client/list', {
        handleAs: 'json'
    }).then(function (response) {
        // Once the response is received, build an in-memory store with the data
        var store = new Memory({ data: response });

        // Create an instance of OnDemandGrid referencing the store
        var grid = new OnDemandGrid({
            store: store,
            sort: 'id', // Initialize sort on id, ascending
            columns: {
                'id': 'ID',
                'number': 'Name',
                'description': 'Description'
            }
        }, 'grid');
        console.log(store);

        on(dom.byId('queryForm'), 'input', function (event) {
            event.preventDefault();
            grid.set('query', {
                // Pass a RegExp to Memory's SimpleQueryEngine
                // Note: this code does not go out of its way to escape
                // characters that have special meaning in RegExps
                description: new RegExp(this.elements.last.value, 'i')
            });
        });

        on(dom.byId('queryForm'), 'reset', function () {
            // Reset the query when the form is reset
            grid.set('query', {});
        });
    });
});

1 个答案:

答案 0 :(得分:0)

好的问题发现:/

我的“客户端/列表”网址正在返回一个像这样的json对象:

{data: [{id:"1", label: "test"}, {id:"2", label: "test"}]}

事实证明,JsonRest对象已经在 data 节点中封装了数据,所以通过这样返回一个json:

{[{id:"1", label: "test"}, {id:"2", label: "test"}]}

一切正常:)