extjs4 - 是否有用于代理的非 json/xml 编写器?

发布于 2024-12-04 07:30:13 字数 496 浏览 1 评论 0原文

我正在构建一些模型来与之前项目中的现有 API 进行交互。

API 依赖标准 POST 方法来保存数据。

我已经配置了一个模型和代理,直到它确实将数据推送到服务器上,但似乎只有两种编写器类型:json 和 json。 xml。

proxy: {
        /* ... */
        reader: {
            type: 'json',
            root: 'results'
        },
        writer: {
            type: '???' // <-- can only see json or xml in the docs
        }
    }

是否有一个标准的 POST 编写器可以简单地在 post 字段中提交数据?

我很惊讶这不是标准的作家类型。

(解析 json 格式不会太难实现,但这意味着更新大量旧的 api 文件。)

I'm building some models to interact with an existing API from a previous project.

The API relies on standard POST methods to save the data.

I've configured a model and proxy up to the point where it does push the data onto the server but there only seems to be two writer types, json & xml.

proxy: {
        /* ... */
        reader: {
            type: 'json',
            root: 'results'
        },
        writer: {
            type: '???' // <-- can only see json or xml in the docs
        }
    }

Isn't there a standard POST writer that simply submits data in post fields?

I'm surprised that wouldn't be a standard writer type.

(Parsing the json format wouldn't be too hard to implement but that would mean updating a lot of the old api files.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红颜悴 2024-12-11 07:30:13

好的,通过检查现有编写器的源代码,我可以很轻松地创建该编写器。

这些现有的编写者能够做的一件事是他们可以一次推送多个记录,这可能就是开发团队只实现 json 和 xml 版本的原因。

这可以在 POST 中实现,但会稍微复杂一些。

如果您尝试使用 POST 将单个模型推送到 api,则此编写器将起作用:

Ext.define('Ext.data.writer.SinglePost', {
    extend: 'Ext.data.writer.Writer',
    alternateClassName: 'Ext.data.SinglePostWriter',
    alias: 'writer.singlepost',

    writeRecords: function(request, data) {
        request.params = data[0];
        return request;
    }
});

并在代理中将其用于编写器:

writer: {
            type: 'singlepost'
        }

Ok, I was able to create that writer quite easily by checking the existing writers' source code.

One thing those existing writers are able to do - and that may be why the dev team only implemented a json and xml version - is that they can push multiple records at once.

That could be implemented in POST but would be a bit more complicated.

This writer will work if you're trying to push a single model to an api using POST:

Ext.define('Ext.data.writer.SinglePost', {
    extend: 'Ext.data.writer.Writer',
    alternateClassName: 'Ext.data.SinglePostWriter',
    alias: 'writer.singlepost',

    writeRecords: function(request, data) {
        request.params = data[0];
        return request;
    }
});

and the use this for the writer in the proxy:

writer: {
            type: 'singlepost'
        }
吃→可爱长大的 2024-12-11 07:30:13

根据 Ben 的回答,我已经实现了自己的编写器,它将所有模型的所有属性收集到数组中。
例如,如果您有一些字段的模型:

fields:[
  {name:'id', type:'int'}
  {name:'name', type:'string'}
  {name:'age', type:'date'}
]

请求字符串将是

id=1&id=2&id=...&name=oleks&name=max&name=...&age=...

代码:

Ext.define('Ext.data.writer.SinglePost', {
    extend: 'Ext.data.writer.Writer',
    alternateClassName: 'Ext.data.SinglePostWriter',
    alias: 'writer.singlepost',
    writeRecords: function(request, data) {
        if(data && data[0]){
            var keys = [];
            for(var key in data[0]){
                keys.push(key);
            }
            for(var i=0;i<keys.length;i++){
                request.params[keys[i]] = [];
                for(var j=0;j<data.length;j++){
                request.params[keys[i]].push((data[j])[keys[i]]);
            }
        }
        }
        return request;
    }
});

Based on Ben answer I've implemented my own writer that will collect all properties of all models into arrays.
For example if you have model like with some fields:

fields:[
  {name:'id', type:'int'}
  {name:'name', type:'string'}
  {name:'age', type:'date'}
]

A request string will be

id=1&id=2&id=...&name=oleks&name=max&name=...&age=...

Code:

Ext.define('Ext.data.writer.SinglePost', {
    extend: 'Ext.data.writer.Writer',
    alternateClassName: 'Ext.data.SinglePostWriter',
    alias: 'writer.singlepost',
    writeRecords: function(request, data) {
        if(data && data[0]){
            var keys = [];
            for(var key in data[0]){
                keys.push(key);
            }
            for(var i=0;i<keys.length;i++){
                request.params[keys[i]] = [];
                for(var j=0;j<data.length;j++){
                request.params[keys[i]].push((data[j])[keys[i]]);
            }
        }
        }
        return request;
    }
});
暗藏城府 2024-12-11 07:30:13

对于 Sencha touch 2.0,将 writeRecords 方法更改为:

writeRecords: function (request, data) {
        var params = request.getParams() || {};
    Ext.apply(params, data[0]);
    request.setParams(params);
    return request;
}

For Sencha touch 2.0, change the writeRecords method to:

writeRecords: function (request, data) {
        var params = request.getParams() || {};
    Ext.apply(params, data[0]);
    request.setParams(params);
    return request;
}
场罚期间 2024-12-11 07:30:13

这是我的版本,改编自上面的答案:

// Subclass the original XmlWriter
Ext.define('MyApp.utils.data.writer.XmlInAPostParameter', {

    extend : 'Ext.data.writer.Xml',

    // give it an alias to use in writer 'type' property
    alias : 'writer.xml_in_a_post_parameter',

    // override the original method
    writeRecords : function(request, data) {

        // call the overriden method - it will put the data that I 
        //   want into request.xmlData
        this.callParent(arguments);

        // copy the data in request.xmlData. In this case the XML 
        // data will always be in the parameter called 'XML'
        Ext.apply(request.params, {
            XML: request.xmlData
        });

        // Already copied the request payload and will not send it, 
        //   so we delete it from the request
        delete request.xmlData;

        // return the modified request object
        return request;
    }
});

Ext.define("MyApp.model.MyModel", {
    extend : "Ext.data.Model",
    requires : [ 
        'MyApp.utils.data.writer.XmlInAPostParameter' 
    ],

    fields : [ 'field_A', 'field_B' ],

    proxy : {
        type : 'ajax',

        api : {
            read : '/mymodel/read.whatever',
            update : '/mymodel/write.whatever'
        },

        reader : {
            type : 'xml'
        },
        writer : {

            // use the alias we registered before
            type : 'xml_in_a_post_parameter'
        }
    }

});

Here's my version, adapted from answers above:

// Subclass the original XmlWriter
Ext.define('MyApp.utils.data.writer.XmlInAPostParameter', {

    extend : 'Ext.data.writer.Xml',

    // give it an alias to use in writer 'type' property
    alias : 'writer.xml_in_a_post_parameter',

    // override the original method
    writeRecords : function(request, data) {

        // call the overriden method - it will put the data that I 
        //   want into request.xmlData
        this.callParent(arguments);

        // copy the data in request.xmlData. In this case the XML 
        // data will always be in the parameter called 'XML'
        Ext.apply(request.params, {
            XML: request.xmlData
        });

        // Already copied the request payload and will not send it, 
        //   so we delete it from the request
        delete request.xmlData;

        // return the modified request object
        return request;
    }
});

Ext.define("MyApp.model.MyModel", {
    extend : "Ext.data.Model",
    requires : [ 
        'MyApp.utils.data.writer.XmlInAPostParameter' 
    ],

    fields : [ 'field_A', 'field_B' ],

    proxy : {
        type : 'ajax',

        api : {
            read : '/mymodel/read.whatever',
            update : '/mymodel/write.whatever'
        },

        reader : {
            type : 'xml'
        },
        writer : {

            // use the alias we registered before
            type : 'xml_in_a_post_parameter'
        }
    }

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文