View Full Version : Loading a single entity with a store or directly with a model ?

18 Jun 2011, 2:01 AM
In ExtJs 4 you can define a proxy on a Store or directly in a Model. For collections the obvious solution will be to load data using a Store but when you have a single entity that needs to be loaded from the server side you can do this with a model Model or with a Sotre.

If we look in the examples from data guide (http://docs.sencha.com/ext-js/4-0/#/guide/data)

Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name', 'age', 'gender'],
proxy: {
type: 'rest',
url : 'data/users',
reader: {
type: 'json',
root: 'users'
User.load(1, {
success: function(user) {
console.log("Loaded user 1: " + user.get('name'));

we will see that we have this convenient static method ("load( Number id, [Object config] ) : void STATIC") that accepts also the ID of the object that will be loaded,and that will pas this ID as a parameter to the request

When we want to load a single entity form the server via a Store that has the same Proxy and the same model associated, the load method ("load( Object/Function options ) : void") is not accepting any more an ID as a parameter, and you have to pass the ID as a external param to the config element (something like store.load({params : {id:1}})).

Why this two "load" methods are not providing also similar interfaces (they have similar functionality, they can load a single entity using the attached Proxy) ?

What is the recommended approach or pattern for loading a single entity form the server?

Thank you in advance.

20 Jun 2011, 6:55 PM
I define the proxy on the store, use same model sometimes for different stores and then use proxy.extraParams to set additional post/get params before calling store load.