Created
September 29, 2016 07:13
-
-
Save ReeganExE/048b8eda7cc376450a309b2c436e0e0e to your computer and use it in GitHub Desktop.
An example of ExtJS Model and Store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An example of ExtJS Model and Store | |
* @author Ninh Pham | |
*/ | |
Ext.define('Patient', { | |
extend: 'Ext.data.Model', | |
idProperty: '_id', | |
requires: ['Ext.data.identifier.Uuid'], | |
identifier: 'uuid', | |
proxy: { | |
type: 'rest', | |
url: 'http://localhost:5566/patients', | |
actionMethods: { | |
create: 'POST', // Default | |
read: 'GET', // Default | |
update: 'PATCH', // Default is PUT, json-server wants PATCH | |
destroy: 'DELETE' // Default | |
} | |
} | |
}); | |
Ext.define('PatientStore', { | |
extend: 'Ext.data.Store', | |
alias: 'store.tada', | |
model: 'Patient', | |
autoLoad: true // data will be loaded automatically when store is created | |
}); | |
Ext.create('PatientStore').on('load', function(store, patients) { | |
console.log('All patients:', patients); | |
// Add a new patient | |
store.add(new Patient({ | |
name: 'Dep Trai Co Gi Sai', | |
phone: '0303030303', | |
location: 'Address 003', | |
sex: 'Female', | |
birthDate: '03/30/1993' | |
})); | |
// Sync this store to server side | |
// Note: we can set autoSync: true, then we don't need following sync() | |
store.sync(); | |
// Example of update patient | |
var firstPatient = store.getAt(0); | |
// set a single value | |
firstPatient.set('name', 'new namemmmm'); | |
// set multiple fields | |
firstPatient.set({ | |
phone: '444', | |
sex: 'Male', | |
}); | |
// Save the first patient | |
// This will save to server side | |
firstPatient.save(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment