Using Dojo's dgrid, JsonRest and subrows/arrays

£可爱£侵袭症+ 提交于 2019-12-02 03:01:24

I can see multiple options here, but the most straightforward would be writing your own Store satisfying the interface of dojo/store/api/Store or just hack it and subclass dojo/store/JsonStore:

var CustomStore = declare(JsonRest, {
    query: function(query, options) {
        var dataProperty = this.dataProperty;
        var results = this.inherited(arguments);
        var deferred = results.then(function(result) {
            return result[dataProperty];
        });
        return QueryResults(deferred);
    }           
});

then you will need to add one more property when instantiating - dataProperty:

var store = new CustomStore({
    target: "/visitors/",
    idProperty: "num",
    dataProperty: "visitors"
});

See it in action at jsFiddle: http://jsfiddle.net/phusick/MG9jB/

Other option would be to change the response before it reaches dojo/store/JsonRest, so JsonRest gets what it expects, an array. Dojo 1.8 provides dojo/request which employs XHR2 thus it unfortunately does not work with JsonRest, but just for the sake of elegancy:

// var request = require("dojo/request/registry");
// var xhr = require("dojo/request/xhr");

var handle = request.register(/(.*)\/visitors.json$/, function(url, options) {
    // if any XHR request url ends with `visitors.json` then return
    // `visitors` property
    return xhr.get(url, options).then(function(results) {
        return results["visitors"];
    });
});

request.get("app/visitors.json", {handleAs: "json"}).then(function(visitors) {
    console.log(visitors);
});

In the article Introducing dojo/request one can find a reference to dojox/io/xhrPlugins which should provide similar functionality against legacy code. And even if it does not, you can use dojo/aspect or possibly write your own content handler to achieve the same.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!