ExtJS - 如何使用代理、模型?它们有什么关系? [英] ExtJS - How to use Proxy, Model? How are they related?

查看:28
本文介绍了ExtJS - 如何使用代理、模型?它们有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力学习使用模型和商店.但是代理位让我很困惑.所以我将在这里列出我的理解 - 请指出我的理解中的差距.

我的理解

  1. 模型用于表示领域对象.
  2. 模型可以通过 ModelManager 创建,或者简单地使用构造函数
  3. 模型保存在 Stores 中
  4. 存储可以在内存存储中,也可以是服务器存储.这是使用代理配置的.
  5. 代理告诉商店如何与后备商店对话 - 无论是 JSON 数组、REST 资源,还是通过 ajax 简单配置的 URL.
  6. 商店负责存储模型,代理负责控制/帮助完成该任务.
  7. 当模型的值改变时,它的 dirty 标志被设置.保存模型时它会自动清除.(稍后会详细介绍)

让我困惑的部分

  1. 为什么模型上有 proxy 配置和 save 方法?我了解模型只能存储到商店.
  2. 为什么在我将模型对象添加到商店时没有简单地清除 dirty 标志?
  3. 当我向商店添加模型对象时,为什么模型没有获取该商店配置的代理?
  4. proxy 是模型的静态配置.这是否意味着我们不能使用具有多个数据源的特定模型的对象?推而广之,这是否意味着为单个模型设置多个商店基本上毫无用处?
  5. 当我们定义一个 Store 时,我们是在定义一个类(store-type,如果我们可以这样称呼它),还是一个 store 的实例?我问的原因是当我们声明一个网格时,我们只需将一个商店配置作为 store: 'MyApp.store.MyStore' 传递给它 - 网格是否实例化该类型的网格,还是只是使用我们已经实例化的商店?

谢谢!

PS:+50 赏金给解释这一切的人:) - 48 小时结束后将提供赏金..

解决方案

文档 说:

<块引用>

模型代表您的应用程序管理的某个对象.

Store 只是模型实例的集合 - 通常从某处的服务器加载.


<块引用>

模型保存在 Stores 中

不仅如此.模型可以单独使用(fi 用于用数据填充表单.查看 Ext.form.Panel.loadRecord 了解更多信息.

<块引用>

为什么模型上有代理配置和保存方法?我了解模型只能存储到商店.

正如我所说,不仅如此.

<块引用>

为什么当我将模型对象添加到商店时不清除脏标志?

为什么要这样?记录只有在与服务器端的相应记录同步后才变得干净.

<块引用>

当我将模型对象添加到商店时,为什么模型没有获取该商店配置的代理?

这也是因为模型可以在没有存储的情况下使用.

<块引用>

代理是模型的静态配置.这是否意味着我们不能使用具有多个数据源的特定模型的对象?

我们不能使用特定模型的对象,但我们可以为多个商店使用一个模型定义.例如:

Ext.define('MyModel', {//... 配置});var store1 = Ext.create('Ext.data.Store', {模型:'我的模型',//... 配置代理人: {//...//当 store1.sync() 和 store1.load() 被调用时会使用这个代理}//...});//...var storeN = Ext.create('Ext.data.Store', {模型:'我的模型',//... 配置代理人: {//...//调用 storeN.sync() 和 storeN.load() 时将使用此代理}//...});

由于 store1 和 storeN 使用不同的代理,所以这些 store 包含的记录可能完全不同.

<块引用>

当我们定义一个 Store 时,我们是在定义一个类(store-type,如果我们可以这样称呼它),还是一个 store 的实例?

是的,当我们定义一个 Store 时,我们就是在定义一个类.

<块引用>

我问的原因是当我们声明一个网格时,我们只是将一个商店配置作为 store 传递给它:'MyApp.store.MyStore' - 网格是否实例化了该类型的网格,或者它只是使用我们的商店'已经实例化了吗?

有几种方法可以为网格设置商店配置:

  1. store: existingStore,
  2. store: 'someStoresId',
  3. store: 'MyApp.store.MyStore',

在第一种和第二种情况下,将使用商店的现有实例.在第三种情况下,将使用新创建的 'MyApp.store.MyStore' 实例.所以,store: 'MyApp.store.MyStore', 等于

 var myStore = Ext.create('MyApp.store.MyStore', {});//...//以下 - 在网格的配置中:商店:我的商店,


更新


<块引用>

当一个模型被添加到商店然后调用商店的sync()时,为什么模型的脏标志没有被清除?

它应该被清除,除非读取器、写入器和服务器响应设置不正确.查看编写器示例.store 的 sync() 上自动清除了脏标志.


<块引用>

如果一个模型类根本没有被赋予任何代理,它为什么要跟踪它的脏状态?

让您知道自创建那一刻起记录是否已更改.


<块引用>

引入模型将自身同步到服务器的概念可以实现什么?

让我们假设您正在创建一个包含简单输入集的小部件.此输入的值可以从 db 加载(这组输入代表 db 表中的一行).当用户更改输入值时,数据应发送到服务器.这些数据可以存储在一个记录中.

那么你会为这个界面使用什么:一条记录​​还是只有一条记录的商店?

独立模型 - 用于代表数据库表中一行的小部件.
Store - 用于表示 db 表中的一组行的小部件.

I've been trying to learn to work with Models and Stores. But the proxy bit is confusing me a lot. So I'm going to list out my understanding here - please point out the gaps in my understanding.

My understanding

  1. Models are used to represent domain objects.
  2. Models can be created by ModelManager, or by simply using the constructor
  3. Models are saved in Stores
  4. Stores may be in memory stores, or can be server stores. This is configured using Proxy.
  5. Proxy tells the store how to talk to a backing store - be that a JSON array, or a REST resource, or a simply configured URL via ajax.
  6. Stores are responsible for storing models, and Proxies are responsible for controlling/helping with that task.
  7. When a model's values are changed, its dirty flag gets set. It gets automatically cleared when the model is saved. (more in this later)

The part that confuses me

  1. Why is there a proxy config and save method on Model? I understand that models can only be stored on to stores.
  2. Why is the dirty flag not cleared simply when I add a model object to a store?
  3. When I add a model object to a store, why does the model not acquire the proxy configured with that store?
  4. proxy is a static configuration for a Model. Does that mean that we cannot use objects of a particular model with multiple data sources? By extension, does this mean having multiple stores for a single model essentially useless?
  5. When we define a Store, are we defining a class (store-type, if we may call it that), or is it an instance of a store? Reason I ask is when we declare a grid, we simply pass it a store config as store: 'MyApp.store.MyStore' - does the grid instantiate a grid of that type, or is it simply using the store we've already instantiated?

Thanks!

PS: +50 bounty to the person who explains all this :) - will offer bounty after those 48 hours are over..

解决方案

The docs say:

A Model represents some object that your application manages.

A Store is just a collection of Model instances - usually loaded from a server somewhere.


Models are saved in Stores

Not only. The Models can be used separately (f.i. for populating forms with data. Check out Ext.form.Panel.loadRecord for more info).

Why is there a proxy config and save method on Model? I understand that models can only be stored on to stores.

As I've said, not only.

Why is the dirty flag not cleared simply when I add a model object to a store?

Why should it? The Record becomes clean only when it gets synchronized with the corresponding record on the server side.

When I add a model object to a store, why does the model not acquire the proxy configured with that store?

This is, again, because model can be used without store.

proxy is a static configuration for a Model. Does that mean that we cannot use objects of a particular model with multiple data sources?

We cannot use objects of a particular model but we can use one model definition for multiple store. For example:

Ext.define('MyModel', {
  // ... config
});
var store1 = Ext.create('Ext.data.Store', {
  model: 'MyModel',
  // ... config
  proxy: {
    // ...
    // this proxy will be used when store1.sync() and store1.load() are called
  }
  // ...
});
// ...
var storeN = Ext.create('Ext.data.Store', {
  model: 'MyModel',
  // ... config
  proxy: {
    // ...
    // this proxy will be used when storeN.sync() and storeN.load() are called
  }
  // ...
});

Since store1 and storeN use different proxies, the records, contained by these stores, may be completely different.

When we define a Store, are we defining a class (store-type, if we may call it that), or is it an instance of a store?

Yes, when we define a Store, we are defining a class.

Reason I ask is when we declare a grid, we simply pass it a store config as store: 'MyApp.store.MyStore' - does the grid instantiate a grid of that type, or is it simply using the store we've already instantiated?

There are couple of ways to set store config for grid:

  1. store: existingStore,
  2. store: 'someStoresId',
  3. store: 'MyApp.store.MyStore',

In the first and the second cases existing instances of stores would be used. In the third case newly created instance of 'MyApp.store.MyStore' would be used. So, store: 'MyApp.store.MyStore', is equal to

  var myStore = Ext.create('MyApp.store.MyStore', {});
  // ... 
    // the following - is in a grid's config:
    store: myStore,


UPDATE


When a model is added to store and then the store's sync() is called, why is the model's dirty flag not cleared?

It should be cleared, unless reader, writer and server response are not set up properly. Check out writer example. There is dirty flag being cleared automaticaly on store's sync().


if a model class is not given any proxy at all, why does it track its dirty status?

To let you know if the record was changed since the creation moment.


What is achieved by introducing the concept of Models syncing themselves to the server?

Let's assume you are creating a widget which contains plain set of inputs. The values for this inputs can be loaded from db (this set of inputs represents one row in db table). And when user changes the inputs' values data should be sent to the server. This data can be stored in one record.

So what would you use for this interface: one record or a store with only one record?

Standalone model - is for widgets that represent one row in db table.
Store - is for widgets that represent set of rows in db table.

这篇关于ExtJS - 如何使用代理、模型?它们有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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