Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You won't get what you want from stock Ext classes, you'll have to get a little bit dirty...</p> <p>From what I understand you need the id of the user to load its account, not the id of the account record itself. So, I would configure the association to reflect that:</p> <pre><code>associations: [{ model: "MyApp.model.Account", name: "account", type: "hasOne", reader: "json", getterName: "getAccount", setterName: "setAccount", // foreignKey: "accountId" foreignKey: 'id' }], </code></pre> <p>The big advantage here being that the user id will be available to the proxy when it will be asked to build the url for the request.</p> <p>Now, in order to build the url with the format you need, we must replace the proxy's <a href="http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.proxy.Rest-method-buildUrl" rel="noreferrer"><code>buildUrl</code></a> method. And, as you've already discovered, you do need an url to get to this method in the first place.</p> <p>So, here's how I would configure the Account proxy:</p> <pre><code>proxy: { type: "rest", reader: {type: "json"}, // Give it an URL to avoid the error url: '/rest/users/{}/account', // Replace the buildUrl method buildUrl: function(request) { var me = this, operation = request.operation, records = operation.records || [], record = records[0], url = me.getUrl(request), id = record ? record.getId() : operation.id; // Here's the part honoring your URL format if (me.isValidId(id)) { url = url.replace('{}', id); } else { throw new Error('A valid id is required'); } // 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); } } </code></pre> <p>At this point, we end up with the proxy firing request on urls of the form:</p> <pre><code>rest/users/45/account?id=45 </code></pre> <p>That's only cosmetic but that id query parameter annoys me, so I would also replace the <code>buildRequest</code> method of the proxy with the following one:</p> <pre><code>buildRequest: function(operation, callback, scope) { var me = this, params = operation.params = Ext.apply({}, operation.params, me.extraParams), request; Ext.applyIf(params, me.getParams(operation)); // if (operation.id !== undefined &amp;&amp; params[me.idParam] === undefined) { // params[me.idParam] = operation.id; // } request = new Ext.data.Request({ params : params, action : operation.action, records : operation.records, operation: operation, url : operation.url, proxy: me }); request.url = me.buildUrl(request); operation.request = request; return request; } </code></pre> <p>And, here you are... While it would work, I don't really recommend overriding methods this way, in the proxy's config. In real life, you should extend your own proxy class from the Rest one, especially if you need to configure many such proxies... But I hope I gave you all the ingredients you need to get started!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload