开发者

Should I specify the proxy in the Model or the Store?

I'm relatively new to cross-platform mobile development, and recently have been going through some basic tutorials on how to use Sencha Touch in my app. I have been confused by what seems to me as inconsistency in their methodology. So here is my question:

I am using the MVC design pattern. When defining a proxy for storing data in an app, should I do this in the Model or in the Store? I have seen both done and am not sure which is better, or the cases when each should be used. The following are examples of what I mean:

Proxy defined in the Store:

App.stores.users = new Ext.data.Store({
    model: 'User',
    autoLoad: true,

    proxy: {
         type: 'localstorage',
         id: 'sencha-users'
    }
});

Proxy defined in开发者_开发知识库 the Model:

App.models.User = Ext.regModel('User', {
    fields: [
        {
            name: 'email',
            type: 'string'
        }, {
            name: 'firstname',
            type: 'string'
        }, {
            name: 'lastname',
            type: 'string'
        }, {
            name: 'password',
            type: 'string'
        }
    ],

    proxy: {
        type: 'localstorage',
        id: 'sencha-users'
    }
});


They leave it up to you because it's an implementation decision. The convenience of having it tied to the model dictates a strong one-to-one relationship.

In ExtJS 3.3.1 and below, the proxy was always tied to the store. It wasn't until Sencha Touch and ExtJS 4 that they allowed you to tie directly to the model.

The best reason I can think of to tie it to the model is to keep it self-contained.

Defining Proxies directly on a Model is a new approach in version 4—its main benefit is that we can easily load and save Model data without creating a Store, as we did in ExtJS 3.

Edit

Sorry if I was a little high level with my explanation.

Think of a Store as a Collection of Models.

The store can be self-aware on how to populate these models by using the proxy defined to the store. Or the Model may already know how to populate itself by defining the proxy to it instead.

Please let me know if I didn't clarify well enough. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜