Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / restful / restful.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 Ext.require(['Ext.data.*', 'Ext.grid.*']);
16
17 Ext.define('Person', {
18     extend: 'Ext.data.Model',
19     fields: [{
20         name: 'id',
21         type: 'int',
22         useNull: true
23     }, 'email', 'first', 'last'],
24     validations: [{
25         type: 'length',
26         field: 'email',
27         min: 1
28     }, {
29         type: 'length',
30         field: 'first',
31         min: 1
32     }, {
33         type: 'length',
34         field: 'last',
35         min: 1
36     }]
37 });
38
39 Ext.onReady(function(){
40
41     var store = Ext.create('Ext.data.Store', {
42         autoLoad: true,
43         autoSync: true,
44         model: 'Person',
45         proxy: {
46             type: 'rest',
47             url: 'app.php/users',
48             reader: {
49                 type: 'json',
50                 root: 'data'
51             },
52             writer: {
53                 type: 'json'
54             }
55         },
56         listeners: {
57             write: function(store, operation){
58                 var record = operation.getRecords()[0],
59                     name = Ext.String.capitalize(operation.action),
60                     verb;
61                     
62                     
63                 if (name == 'Destroy') {
64                     record = operation.records[0];
65                     verb = 'Destroyed';
66                 } else {
67                     verb = name + 'd';
68                 }
69                 Ext.example.msg(name, Ext.String.format("{0} user: {1}", verb, record.getId()));
70                 
71             }
72         }
73     });
74     
75     var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');
76     
77     var grid = Ext.create('Ext.grid.Panel', {
78         renderTo: document.body,
79         plugins: [rowEditing],
80         width: 400,
81         height: 300,
82         frame: true,
83         title: 'Users',
84         store: store,
85         iconCls: 'icon-user',
86         columns: [{
87             text: 'ID',
88             width: 40,
89             sortable: true,
90             dataIndex: 'id'
91         }, {
92             text: 'Email',
93             flex: 1,
94             sortable: true,
95             dataIndex: 'email',
96             field: {
97                 xtype: 'textfield'
98             }
99         }, {
100             header: 'First',
101             width: 80,
102             sortable: true,
103             dataIndex: 'first',
104             field: {
105                 xtype: 'textfield'
106             }
107         }, {
108             text: 'Last',
109             width: 80,
110             sortable: true,
111             dataIndex: 'last',
112             field: {
113                 xtype: 'textfield'
114             }
115         }],
116         dockedItems: [{
117             xtype: 'toolbar',
118             items: [{
119                 text: 'Add',
120                 iconCls: 'icon-add',
121                 handler: function(){
122                     // empty record
123                     store.insert(0, new Person());
124                     rowEditing.startEdit(0, 0);
125                 }
126             }, '-', {
127                 itemId: 'delete',
128                 text: 'Delete',
129                 iconCls: 'icon-delete',
130                 disabled: true,
131                 handler: function(){
132                     var selection = grid.getView().getSelectionModel().getSelection()[0];
133                     if (selection) {
134                         store.remove(selection);
135                     }
136                 }
137             }]
138         }]
139     });
140     grid.getSelectionModel().on('selectionchange', function(selModel, selections){
141         grid.down('#delete').setDisabled(selections.length === 0);
142     });
143 });
144