javascript - ExtJS - 如何使用代理、模型?它们有什么关系?

标签 javascript model proxy extjs4 store

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

我的理解

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

让我感到困惑的部分

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

谢谢!

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

最佳答案

The docs说:

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

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

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

正如我所说,不仅如此。

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

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

When I add a model object to a store, why does the model not acquire the proxy configured with that 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?

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

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 使用不同的代理,这些商店包含的记录可能完全不同。

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?

是的,当我们定义 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?

有几种方法可以为网格设置存储配置:

  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', {});
  // ... 
    // the following - is in a grid's config:
    store: myStore,

更新


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

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


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

让您知道记录自创建以来是否发生了更改。


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

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

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

独立模型 - 适用于代表数据库表中一行的小部件。
Store - 用于表示数据库表中行集的小部件。

关于javascript - ExtJS - 如何使用代理、模型?它们有什么关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7404870/

相关文章:

Python/Django 建模问题

docker - 单个域的多个位置的 Nginx 代理

php - Docker:容器之间无法通信

cocoa - 在 Cocoa 中正确设计模型 Controller ?

javascript - 如何通过输入项目 ID 或序列号来查找列表项目文本中的值

javascript - 如何共享 Swagger 文档

javascript - 在悬停时制作一个 div 覆盖链接

ruby-on-rails - 基于可能为零的列的条件验证

proxy - nginx简单的proxy_pass到本地主机不起作用

javascript - 动态设置谷歌脚本时间驱动计时器和gmail中计时器错误太多