我应该在模型或商店中指定代理? [英] Should I specify the proxy in the Model or the Store?

查看:116
本文介绍了我应该在模型或商店中指定代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相对较新的跨平台移动开发,最近一直在通过一些关于如何在我的应用程序中使用Sencha Touch的基础教程。在我看来,我被他们的方法论不一致所困惑。所以这里是我的问题:



我正在使用MVC设计模式。在定义应用程序中存储数据的代理时,应该在模型或商店中执行此操作吗?我已经看到两个都完成了,我不知道哪个更好,或者每个应该被使用的情况。以下是我的意思的例子:



商店中定义的代理:



< pre class =lang-js prettyprint-override> App.stores.users = new Ext.data.Store({
model:'User',
autoLoad:true,

代理:{
类型:'localstorage',
id:'sencha-users'
}
});

模型中定义的代理:

  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'
}
} );


解决方案

他们把它留给你,因为它是一个实现决定。将其绑定到模型的便利性决定了一个很强的一对一关系。



在ExtJS 3.3.1及更低版本中,代理始终与存储。直到Sencha Touch和ExtSJ 4才允许你直接与模型联系。



我认为将其与模型结合的最好的理由是保持自给自足。


直接在模型上定义代理是版本
4中的一种新方法 - 其主要优点是可以轻松加载和保存模型数据
而不创建商店,就像我们在Ext JS 3中所做的那样。


编辑



对不起,如果我是一个高水平的我的解释。



想想一个商店



商店可以自行了解如何使用代理定义填充这些模型到商店。 ,模型可能已经知道如何通过为其定义代理来填充自己。



请让我知道,如果我没有澄清足够好。 :)


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 ExtSJ 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 Ext JS 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. :)

这篇关于我应该在模型或商店中指定代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆