Sencha Model.save将/0.json发送到服务器

时间:2011-03-03 13:45:30

标签: ruby-on-rails sencha-touch extjs

我有以下代码,它应该创建一个新项目。代理类型是REST。

var inst = Ext.ModelMgr.create({
                    title: values.title
                }, "EntriesModel");

                inst.save({
                    success: function(model) {
                        console.log(model);
                    }
                });

在save()之后,我看到该请求已发送到http://localhost:3000/entries/0.json,而我认为它应该已发送到http://localhost:3000/entries

条目模型看起来像这样

Ext.regModel("EntriesModel", {
fields: [
    {name: "id",             type: "int"},
    {name: "title",           type: "string"},
    {name: "list_id",       type:"int"},
    {name: "bought",         type: "boolean"},
],

proxy: {
    type: 'rest',
    url: '/entries',
    format: 'json',
    noCache: true,
    reader: {
        type: 'json',
        root: 'data'
    },
    writer: {
        type: 'json'
    },
    listeners: {
        exception: function (proxy, response, operation) {
            console.log(proxy, response, operation);
        }
    }
}
});

后端是Rails。

2 个答案:

答案 0 :(得分:0)

尝试阅读此http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.RestProxy 例如:

    new Ext.data.RestProxy({
    url: '/users',
    format: 'json'
}); 
// Collection url: /users.json 
// Instance url  : /users/123.json

答案 1 :(得分:0)

看看这个,如何构建一个Rest Proxy的链接:

buildUrl: function(request) {
    var records = request.operation.records || [],
        record  = records[0],
        format  = this.format,
        url     = request.url || this.url;

    if (this.appendId && record) {
        if (!url.match(/\/$/)) {
            url += '/';
        }

        url += record.getId();
    }

    if (format) {
        if (!url.match(/\.$/)) {
            url += '.';
        }

        url += format;
    }

    request.url = url;

    return Ext.data.RestProxy.superclass.buildUrl.apply(this, arguments);
}

覆盖它以提供进一步的自定义,但记得调用超类buildUrl