restful.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Ext.require(['Ext.data.*', 'Ext.grid.*']);
  2. Ext.define('Person', {
  3. extend: 'Ext.data.Model',
  4. fields: [{
  5. name: 'id',
  6. type: 'int',
  7. useNull: true
  8. }, 'email', 'first', 'last'],
  9. validations: [{
  10. type: 'length',
  11. field: 'email',
  12. min: 1
  13. }, {
  14. type: 'length',
  15. field: 'first',
  16. min: 1
  17. }, {
  18. type: 'length',
  19. field: 'last',
  20. min: 1
  21. }]
  22. });
  23. Ext.onReady(function(){
  24. var store = Ext.create('Ext.data.Store', {
  25. autoLoad: true,
  26. autoSync: true,
  27. model: 'Person',
  28. proxy: {
  29. type: 'rest',
  30. url: 'app.php/users',
  31. reader: {
  32. type: 'json',
  33. root: 'data'
  34. },
  35. writer: {
  36. type: 'json'
  37. }
  38. },
  39. listeners: {
  40. write: function(store, operation){
  41. var record = operation.getRecords()[0],
  42. name = Ext.String.capitalize(operation.action),
  43. verb;
  44. if (name == 'Destroy') {
  45. record = operation.records[0];
  46. verb = 'Destroyed';
  47. } else {
  48. verb = name + 'd';
  49. }
  50. Ext.example.msg(name, Ext.String.format("{0} user: {1}", verb, record.getId()));
  51. }
  52. }
  53. });
  54. var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');
  55. var grid = Ext.create('Ext.grid.Panel', {
  56. renderTo: document.body,
  57. plugins: [rowEditing],
  58. width: 400,
  59. height: 300,
  60. frame: true,
  61. title: 'Users',
  62. store: store,
  63. iconCls: 'icon-user',
  64. columns: [{
  65. text: 'ID',
  66. width: 40,
  67. sortable: true,
  68. dataIndex: 'id'
  69. }, {
  70. text: 'Email',
  71. flex: 1,
  72. sortable: true,
  73. dataIndex: 'email',
  74. field: {
  75. xtype: 'textfield'
  76. }
  77. }, {
  78. header: 'First',
  79. width: 80,
  80. sortable: true,
  81. dataIndex: 'first',
  82. field: {
  83. xtype: 'textfield'
  84. }
  85. }, {
  86. text: 'Last',
  87. width: 80,
  88. sortable: true,
  89. dataIndex: 'last',
  90. field: {
  91. xtype: 'textfield'
  92. }
  93. }],
  94. dockedItems: [{
  95. xtype: 'toolbar',
  96. items: [{
  97. text: 'Add',
  98. iconCls: 'icon-add',
  99. handler: function(){
  100. // empty record
  101. store.insert(0, new Person());
  102. rowEditing.startEdit(0, 0);
  103. }
  104. }, '-', {
  105. itemId: 'delete',
  106. text: 'Delete',
  107. iconCls: 'icon-delete',
  108. disabled: true,
  109. handler: function(){
  110. var selection = grid.getView().getSelectionModel().getSelection()[0];
  111. if (selection) {
  112. store.remove(selection);
  113. }
  114. }
  115. }]
  116. }]
  117. });
  118. grid.getSelectionModel().on('selectionchange', function(selModel, selections){
  119. grid.down('#delete').setDisabled(selections.length === 0);
  120. });
  121. });