【问题标题】:Proxy of ExtJS modelExtJS 模型的代理
【发布时间】:2013-11-12 21:28:46
【问题描述】:

我正在使用 ExtJS 4.2。我有以下代码:

Ext.define('Model', {
    extend: 'Ext.data.Model',
    fields: [{
        name: 'id',
        type: 'int'
    }, {
        name: 'type',
        type: 'string'
    }, {
        name: 'type_id',
        type: 'int'
    }],

    proxy: {
        type: 'ajax',
        api: {
            update: 'localhost/update'
        },
        reader: {
            type: 'json',
            root: 'data' 
        },
        writer: {
            root: 'data',
            encode: true
        }
    }
});

var record = new Model({
    id: 100,
    type_id: 2
});
record.phantom = false;

record.save({
     success: function(record) {
          console.log(record.get('type')); 
     }
});

请求本地主机/更新的参数:

数据:{id:100,type_id:2}

回复:

data: {id: 100, type_id: 2, type: '记录类型'}

为什么

console.log(record.get('type'));

显示为空?

【问题讨论】:

    标签: javascript extjs model extjs4.2


    【解决方案1】:

    您可能需要在您的回复中添加"success": true 或在您的阅读器上将successProperty 设置为null

    【讨论】:

    • 为什么?请求成功。 record.get('type_id') 返回 2。
    • 为什么你在我的澄清中删除了你的答案?
    • 因为您的澄清,我意识到您不是要加载记录,而是要保存它。
    • 我建议尝试将success: true 添加到您的响应中,因为如文档链接所示,代理的 json 阅读器默认配置为预期它。我没有通读相关代码以确保它被强制执行,但这是一个可能的原因。关于,您可以从记录中读取type_id这一事实,在您展示的代码中,您在创建模型时自己将其放入其中。
    猜你喜欢
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2015-05-23
    • 2023-04-05
    • 2011-11-16
    • 1970-01-01
    • 2012-08-11
    相关资源
    最近更新 更多