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

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

问题描述

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

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.


  1. 模型用于表示域对象。

  2. 模型可以由ModelManager创建,也可以只使用构造函数

  3. 模型保存在商店中

  4. 商店可能在内存商店中,也可能是服务器商店。这是使用代理配置的。

  5. 代理告诉商店如何与后备存储通信 - 是JSON数组,REST资源,还是通过ajax配置的简单URL。

  6. 商店负责存储模型,代理负责控制/帮助执行该任务。

  7. 当模型的值发生变化时,其标志已设置。保存模型时会自动清除它。 (稍后会详细介绍)

  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. 为什么模型上有代理配置和保存方法?我知道模型只能存储在商店中。

  2. 为什么在添加模型对象时不会清除标志到商店?

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

  4. proxy 是Model的静态配置。这是否意味着我们不能将特定模型的对象与多个数据源一起使用?通过扩展,这是否意味着单个模型的多个商店基本上没用?

  5. 当我们定义商店时,我们是否定义了一个类(商店类型,如果我们可以称之为) ,还是商店的实例?我问的原因是当我们声明一个网格时,我们只是将一个商店配置传递给 store:'MyApp.store.MyStore' - 网格是否实例化一个网格那种类型,还是仅仅使用我们已经实例化的商店?

  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?

谢谢!

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

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

推荐答案

文档说:


模型表示应用程序管理的对象。

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

不仅如此。模型可以单独使用(fi用于填充表格和数据。请查看 Ext.form.Panel.loadRecord 获取更多信息)。

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?

这又是因为模型可以在没有商店的情况下使用。 / p>

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
  }
  // ...
});

由于store1和storeN使用不同的代理,因此这些商店包含的记录可能完全不同。

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.


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

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',

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

在第一个和第二个案例中将使用商店的实例。在第三种情况下,将使用新创建的'MyApp.store.MyStore'实例。所以, 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,






更新


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

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

应该清除它,除非没有正确设置读取器,写入器和服务器响应。查看编写者示例。在商店的sync()上会自动清除脏标志。

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?

假设您正在创建一个包含普通输入集的小部件。可以从db加载此输入的值(这组输入表示db表中的一行)。当用户更改输入值时,应将数据发送到服务器。该数据可以存储在一个记录中。

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?

独立模型 - 用于表示db表中一行的小部件。

存储 - 用于表示db表中行集的小部件。

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天全站免登陆