0
votes

In ExtJS 6.2.1, I'm trying to get a record by using model.load() function, and I'm following the example on Ext.data.Model doc, "Using a Proxy" section.

This is my User model:

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

extend: 'Ext.data.Model',

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

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

});

And in the View, I'm trying to load the user which 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'));  
            }
        });

But it did not send a request to /user/1, it sent a request to /user

I also tried a different approach, set the id when creating the user instance, and reference the id inside User model, but it cannot get the ID.

If I hardcode the ID in the URL, it can get the result, so the main problem is how to get the 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'
    }
}

What is the appropriate way of doing this, do I have to create a store?

1

1 Answers

2
votes

The proxy of type:'ajax', when trying to get the user with id 1, sends a request to

/user?_dc=1498020013907&id=1

If you want it to send a request to

/user/1?_dc=1498020013907

you have to use a proxy of type:'rest'.