-
Notifications
You must be signed in to change notification settings - Fork 7
Ext JS 4 RESTful Model Proxy Store CRUD request response parameters
tayfunoziserikan edited this page Dec 1, 2011
·
5 revisions
##Model, Proxy and Store Definitions##
Ext.define('User', {
extend: 'Ext.data.Model',
fields: ['id', 'name'],
proxy: {
type: 'rest',
url : 'users', // Your service url
reader: {
type: 'json',
root: 'users' // Very important. Root name and model name is same
}
}
});
// Uses the User Model's Proxy
Ext.create('Ext.data.Store', {
model: 'User'
});
##Create new record from server (POST)
REQUEST
// Batch mode: OFF
method: 'POST',
url: 'users',
data: [{id:null, name:"Tayfun"}] // Hashed object
// Batch mode: ON
method: 'POST',
url: 'users',
data: [{
id:null,
name:"Tayfun"
},{
id:null,
name:"Onur"
}]
RESPONSE (success)
// Batch mode: OFF
{
success: true,
msg: "New user was created", // Optional
data: {
id: 1, // Return new user id
name: "Tayfun"
}
}
// Batch mode: ON
{
success: true,
msg: "New user(s) was created", // Optional
data: [{
id: 1, // Return new user id
name: "Tayfun"
},{
id: 2, // Return new user id
name: "Onur"
}]
}
RESPONSE (failure)
{
success: false,
msg: 'Response message'
}
##Load All Data from Server (GET)
method: 'GET',
params: {
page:1 // Pagination state
start:0 // Pagination page number
limit:25 // Pagination limit per/page
}
RESPONSE (success)
// Records found
{
success: true,
users: [
{id: 1, name: 'Tayfun'},
{id: 2, name: 'Onur'}
]
}
// Records is not found
{
success: true,
users: []
}
RESPONSE (failure)
{
success: false,
msg: 'Response message'
}
##Load one record (GET)
REQUEST
url: 'users'
method: 'GET',
params: {
id: 1
}
RESPONSES (success)
// Records found
{
success: true,
users: [
{id: 1, name: 'Tayfun'}
]
}
// Records is not found
{
success: true,
users: []
}
RESPONSE (failure)
{
success: false,
msg: 'Response message'
}
##Update existing record from server (PUT)
REQUEST
// Batch mode: OFF
method: 'PUT',
url: 'users/1',
data:[{id: 1, name: "Tayfun Ozis ERIKAN"}]
// Batch mode: ON
method: 'PUT',
url: 'users/1', // Parameter is firs modified record id
data:[{
id: 1,
name: "Tayfun Ozis ERIKAN"
},{
id: 2,
name: "Onur Ozgur OZKAN"
}]
RESPONSE (success)
// Batch mode: OFF
{
success: true,
msg: "User(s) was updated", // Optional
data: {
id: 1,
name: "Tayfun Ozis ERIKAN" // Return only updated fields
}
}
// Batch mode: ON
{
success: true,
msg: "User(s) was updated", // Optional
data: [{
id: 1,
name: "Tayfun Ozis ERIKAN" // Return only updated fields
},{
id: 2,
name: "Onur Ozgur OZKAN" // Return only updated fields
}]
}
RESPONSE (failure)
{
success: false,
msg: 'Response message'
}
##Destroy any record from server (DELETE)
REQUEST
// Batch mode: OFF
method: 'DELETE',
url: 'users/1',
data:{id: 1}
// Batch Mode: ON
method: 'DELETE',
url: 'users/1',
data: [{id:1},{id:2}]
RESPONSE (success)
{
success: true,
msg: "User(s) was deleted", // Optional
data: []
}
RESPONSE (failure)
{
success: false,
msg: 'Response message'
}