【问题标题】:ExtJs load model by IDExtJs 按 ID 加载模型
【发布时间】:2017-06-21 12:42:22
【问题描述】:

在 ExtJS 6.2.1 中,我试图通过使用 model.load() 函数来获取记录,并且我正在遵循 Ext.data.Model doc“使用代理”部分的示例。

这是我的用户模型:

Ext.define('myapp.model.User', {

extend: 'Ext.data.Model',

fields: [
    'id',
    'name',
    'email'
],

proxy: {
    type: 'ajax',
    api: {
        read: '/user'
    },
    reader: {
        type: 'json',
        rootProperty: 'data'
    }
}

});

在视图中,我正在尝试加载 id=1 的用户

    var user = Ext.create('myapp.model.User');
    user.load(1,{
            //scope: this,
            callback: function(record, operation, success) {
                Ext.getCmp('name').setHtml(record.get('name'));
                Ext.getCmp('email').setHtml(record.get('email'));  
            }
        });

但它没有向/user/1发送请求,而是向/user发送了请求

我也尝试了不同的方法,在创建用户实例的时候设置id,在User模型中引用id,但是获取不到ID。

如果我硬编码URL中的ID,它可以得到结果,所以主要的问题是如何获取ID。

var user = Ext.create('myapp.model.User',{id:1}); 
user.load({
        //scope: this,
        callback: function(record, operation, success) {
                Ext.getCmp('name').setHtml(record.get('name'));
                Ext.getCmp('email').setHtml(record.get('email'));  
            }
        });



proxy: {
    type: 'ajax',
    api: {
        read: '/user/1' // this is working 
        //read: '/user/' + this.id // undefined
        //read: '/user/' + this.get('id') //got error: this.get is not a function
    },
    reader: {
        type: 'json',
        rootProperty: 'data'
    }
}

这样做的适当方法是什么,我必须创建一个商店吗?

【问题讨论】:

    标签: javascript extjs


    【解决方案1】:

    type:'ajax'的代理,在尝试获取id为1的用户时,向

    /user?_dc=1498020013907&id=1
    

    如果你想让它发送请求到

    /user/1?_dc=1498020013907
    

    你必须使用type:'rest'的代理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多