javascript EXT JS Store 的代理:读者和作者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8310047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:00:30  来源:igfitidea点击:

EXT JS Store's Proxy: readers and writers

javascriptjqueryhtmlextjsextjs4

提问by Muzaaya Joshua

In the documentation, i have found a store instantiated like this:

在文档中,我发现了一个像这样实例化的商店:

var store = Ext.create('Ext.data.Store', {
    autoLoad: true,
    model: "User",
    proxy: {
        type: 'ajax',
        url : 'users.json',
        reader: {
            type: 'json',
            root: 'users'
        }
    }
});

The proxy has one urlconfig. I am particularly interested in the reader. The reader specifies the data exchange format (json) and the root ('users'). Now, in other words if the store is set up to be: autoLoad = true, then EXT JS will make an Ajax connection to the url specified in order to read. Now, how would i configure a writer for that same store above? Someone also tell me about this: if i configure a writer, would it use the same url as specified in the proxy? am still confused about writers and readers in context of the code i have showed above, you would help me use the above example to show readers and writer configs. Thank you.

代理有一个url配置。我对读者特别感兴趣。读取器指定数据交换格式 (json) 和根 ('users')。现在,换句话说,如果 store 设置为: autoLoad = true,那么 EXT JS 将与指定的 url 建立 Ajax 连接,以便read. 现在,我将如何为上面的同一家商店配置作家?有人也告诉我这个:如果我配置一个 writer,它会使用与代理中指定的相同的 url 吗?我仍然对我上面展示的代码上下文中的作者和读者感到困惑,你会帮助我使用上面的例子来展示读者和作者的配置。谢谢你。

回答by dbrin

Here is an example of a store with reader, writer and api in my App:

这是我的应用程序中具有读取器、写入器和 api 的商店示例:

Ext.define('MyApp.store.Tasks', {
    extend: 'Ext.data.Store',
    model: 'MyApp.model.Task',
    sorters : [{
       property: 'idx',
       direction: 'ASC'
    }],
    autoSync:true,
    proxy:{
        type: 'ajax',
        reader: {
            type: 'json',
            root: 'data'
        },
        writer: {
            type: 'json',
            writeAllFields : false,  //just send changed fields
            allowSingle :false      //always wrap in an array
           // nameProperty: 'mapping'
       },
        api: {
               // read:
                create: 'task/bulkCreate.json',
                update: 'task/bulkUpdate.json'
               // destroy:
        }
    },
    listeners : {
        write: function(store, operation, opts){
            console.log('wrote!');
            //workaround to sync up store records with just completed operation
            Ext.each(operation.records, function(record){
                if (record.dirty) {
                    record.commit();
                }
            });
        },
        update:function(){
            console.log('tasks store updated');
        }
    }
});

回答by Andrey Selitsky

Actually you are correct - it will use the same url as for reader.

实际上你是对的 - 它将使用与阅读器相同的网址。

Proxy is a mediator between your model/store on a client and your server code on another side. Readers are used for reading data and you could configure stuff like formatting, specify root etc. Writers are in charge of save/update requests to the server.

代理是客户端上的模型/商店与另一端的服务器代码之间的中介。Readers 用于读取数据,您可以配置格式、指定 root 等内容。Writers 负责对服务器的保存/更新请求。

Check this article: http://edspencer.net/2011/02/proxies-extjs-4.html

检查这篇文章:http: //edspencer.net/2011/02/proxies-extjs-4.html