Skip to content

Instantly share code, notes, and snippets.

@2xyo
Created May 24, 2012 06:56
Show Gist options
  • Save 2xyo/2779912 to your computer and use it in GitHub Desktop.
Save 2xyo/2779912 to your computer and use it in GitHub Desktop.
Sencha Rest Proxy
Ext.require(['Ext.data.*', 'Ext.grid.*']);
Ext.define('Equipement', {
extend: 'Ext.data.Model',
fields: [{
name: '_id',
type: 'int',
useNull: true
}, 'name', 'plateforme']
});
Ext.onReady(function(){
var store = Ext.create('Ext.data.Store', {
autoLoad: true,
autoSync: true,
model: 'equipement',
proxy: {
type: 'ajax',
url: '/equipements',
appendId: true, //default
noCache: false,
limitParam: false,
enablePagingParams: false,
startParam: false
},
listeners: {
write: function(store, operation){
var record = operation.getRecords()[0],
name = Ext.String.capitalize(operation.action), verb;
if (name == 'Destroy') {
record = operation.records[0];
verb = 'Destroyed';
} else {
verb = name + 'd';
}
Ext.example.msg(name, Ext.String.format("{0} user: {1}", verb, record.getId()));
}
}
});
var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');
var grid = Ext.create('Ext.grid.Panel', {
renderTo: document.body,
plugins: [rowEditing],
width: 800,
height: 600,
frame: true,
title: 'Users',
store: store,
iconCls: 'icon-user',
columns: [{
text: 'ID',
width: 40,
sortable: true,
dataIndex: '_id'
field: {
xtype: 'textfield'
}
}, {
text: 'Email',
flex: 1,
sortable: true,
dataIndex: 'name',
field: {
xtype: 'textfield'
}
}, {
header: 'First',
width: 80,
sortable: true,
dataIndex: 'plateforme',
field: {
xtype: 'textfield'
}
}],
dockedItems: [{
xtype: 'toolbar',
items: [{
text: 'Add',
iconCls: 'icon-add',
handler: function(){
// empty record
store.insert(0, new Equipement());
rowEditing.startEdit(0, 0);
}
}, '-', {
itemId: 'delete',
text: 'Delete',
iconCls: 'icon-delete',
disabled: true,
handler: function(){
var selection = grid.getView().getSelectionModel().getSelection()[0];
if (selection) {
store.remove(selection);
}
}
}]
}]
});
grid.getSelectionModel().on('selectionchange', function(selModel, selections){
grid.down('#delete').setDisabled(selections.length === 0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment