任何人都可以告诉我,我们应该在哪里编写创建/更新等代码片段.我是在代理的api属性下编写的.如何验证输出.请指导我如何使用商店api.
我正在测试,所以请让我更清楚地了解和使用这些功能
Aci*_*ier 16
配置本身在文档中描述.但听起来你的语法是正确的.
您应该知道的第一件事是此配置仅适用于扩展的代理Ext.data.proxy.Server
.请阅读此处的"代理类型"部分.
通过在此配置中定义不同的URL,您只需告诉商店在哪里发送ajax请求以在服务器端执行不同的CRUD操作.
例如,调用store.load()
将ajax请求发送到api.read
URL,由您来确保此URL正确返回数据.
一个Ext.data.Store
内部跟踪的"脏"的记录(创建,更新或销毁)所执行的其他行动.基于此,它将向适当的api
配置URL 发送ajax请求.或者,如果您执行了不同类型的操作,例如创建和删除的记录,它将发送多个ajax请求(每个URL一个)以及有关您创建或删除的记录的数据.
下面是一些示例代码来说明这一点(如果您填写自己的URL,也可以用于测试data.model
).该示例使用默认的读取器/写入器,它将数据作为JSON发送到服务器(代理中有配置以指定不同的格式).
var myStore = Ext.create('Ext.data.Store', {
model: 'MyApp.model.SomeModel',
proxy: {
type: 'ajax',
// without api defined ALL ajax calls will use the 'url' config
url: '/some/url',
api: {
create: '/some/url/to/insert/records/in/db',
read: '/some/url/to/select/records/from/db',
update: '/some/url/to/update/records/in/db',
destroy: '/some/url/to/delete/records/in/db'
}
}
}
// this calls the api.read URL
myStore.load();
// assuming we now have records, this will delete the first record
// on the client side (it will error if there are not records)
myStore.remove(myStore.first());
// the store knows we deleted a record so this will call the api.destroy URL
myStore.sync();
// this updates the first record on the client side
myStore.first().set('some_field_name', 'a string value');
// now we are creating a record on the client side
myStore.add(Ext.create('MyApp.model.SomeModel'));
// the store knows we updated AND created a record so this will call the
// api.update URL AND the api.create URL
myStore.sync();
Run Code Online (Sandbox Code Playgroud)
关于此的另外两个有用的信息:
这里有一个代理配置batchActions
,在文档中有所描述.这是true
在默认情况下,这意味着当你发送请求到数据库中某一类型的所有CRUD行动分为数组.例如,如果您删除了4条记录,那么您的
api.destroy
URL将不会收到4条ajax请求,它将收到1条带有4条记录数组的ajax请求.只要您配置URL来处理阵列,这有助于减少网络流量.您可以将此配置设置为false
,商店将向api.destroy
URL 发送4个请求.您还可以设置编写器的allowSingle
配置(此处描述)以确保所有请求都作为数组发送,即使只有一条记录(这样您可以设置服务器端代码以始终处理数组).
Ext.data.Store
设置为处理创建和更新操作的回调,您只需确保您的URL发回一个.当您致电时myStore.sync()
,商店会自动将客户端的记录替换为您在回调中发送的记录.这对于创建的记录非常有用,因为您可以使用创建的记录发回正确的数据库ID,并在客户端提供该数据库ID,稍后如果用户想要编辑或删除具有正确数据库ID的记录,则可以在以后使用.您还可以在服务器端执行其他处理并发回其他数据,以便您拥有完整的记录(例如,我有时会发回创建用户ID并创建时间).
归档时间: |
|
查看次数: |
10824 次 |
最近记录: |