Accessing complex REST resources with Ext JS

后端 未结 2 1367
攒了一身酷
攒了一身酷 2020-12-23 15:32

I am accessing a REST service which exposes these two resources, a parent resource and a child resource:

/users
/users/{userId}/account

So

2条回答
  •  庸人自扰
    2020-12-23 16:05

    I had the same problem and I found rixo's answer definitely amazing. Therefore I adopted it for myself, but then I made some modifications, so this is the code I'm using at the moment. The advantage is that it allows you to format the service URL absolutely as you prefer, concatenating even more that one parameter.

    // Replace the buildUrl method
                    buildUrl: function (request) {
                        var me = this,
                            url = me.getUrl(request);
                        var added = [];
                        for(var p in request.params)
                        {
                            if (url.indexOf('{' + p + '}') >= 0) {
                                url = url.replace('{' + p + '}', request.params[p]);
                                added.push(p);
                            }
                        }
                        for(var a in added)
                        {
                            delete request.params[added[a]];
                        }
    
                        // That's enough, but we lose the cache buster param (see bellow)
                        return url;
    
                        // If we want the cache buster param (_dc=...) to be added,
                        // we must call the superclass, which will read the url from
                        // the request.
                        request.url = url;
                        return Ext.data.proxy.Rest.superclass.buildUrl.apply(this, arguments);
                    }
    

    This way you can use an url like "/service/{param1}/{param2}/?abc={param3}" given a "request.params" object like

    { "param1": "value1", "param2": "value2", "param3": "value3" }

    and also there's no need to override the "buildRequest" method since any parameter used here is removed from the "params" object and is not concatenated to the query string later.

    I hope this helps, any comment welcome!

提交回复
热议问题