Titouan de Bailleul
Titouan de Bailleul

Reputation: 12949

How to use api attribute on proxy

I would like to know how to use the api attribute of a proxy in ST2

For now, I have this in my proxy configuration:

api: {
    create  : App.urls.create_object,
    read    : App.urls.load_object,
    update  : App.urls.update_object,
    destroy : App.urls.destroy_object
}

But then, I don't know how to use it. For instance, when I wanted to create a new object, I created an Ext.Ajax.request with these parameters :

url: App.urls.create_object,
params: {
    'object': object
},

But now, how could I do the same with the api attribute ?

Could you help ?

Upvotes: 0

Views: 3044

Answers (1)

rdougan
rdougan

Reputation: 7225

Assuming you have a model like this:

Ext.define('User', {
    fields: ['name', 'email'],
    proxy: {
        type: 'ajax',
        api: {
            create: 'my_create_url',
            read: 'my_read_url',
            update: 'my_update_url',
            destroy: 'my_destroy_url'
        }
    }
});

create

var user = Ext.create('User', {name: 'Ed Spencer', email: '[email protected]'});

user.save(); // will POST to the create url

update

var user = Ext.create('User', {name: 'Ed Spencer', email: '[email protected]'});
user.save({
    success: function(user) {
        user.set('name', 'Robert Dougan');

        user.save(); // will PUT update URL
    }
});

read

Using a store:

var store = Ext.create('Ext.data.Store', {
    model: 'User'
});

store.load(); // will GET to read URL

Using the model:

// will GET the read URL with the specified ID.
User.load(12, {
    success: function(user) {
        console.log(user);
    }
});

destroy

var user = Ext.create('User', {name: 'Ed Spencer', email: '[email protected]'});
user.save({
    success: function(user) {
        user.destroy(); // will DELETE destroy URL
    }
});

There is more information about this on the Rest proxy in the Sencha Docs: http://docs.sencha.com/touch/2-0/#!/api/Ext.data.proxy.Rest

sync

You can also use the store sync method to batch create/update/destroy all the records in your store.

var store = Ext.create('Ext.data.Store', {
    model: 'User'
});

store.add({ name: 'Robert Dougan', email: '[email protected]' });

store.sync(); // will batch update all the needed records

Upvotes: 1

Related Questions