app.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * @example Model with Proxy
  3. *
  4. * This example demonstrates using a {@link Ext.data.proxy.Proxy} directly on a {@link Ext.data.Model}
  5. * The data is loaded and saved using a dummy rest api at url `data/users`. You should see messages appear
  6. * in your console when the data is loaded and saved.
  7. * A global variable called "userStore" is created which is an instance of {@link Ext.data.Store}.
  8. * Feel free to experiment with the "userStore" object on the console command line.
  9. */
  10. Ext.define('User', {
  11. extend: 'Ext.data.Model',
  12. fields: ['id', 'name', 'age'],
  13. proxy: {
  14. type: 'rest',
  15. url : 'data/users',
  16. reader: {
  17. type: 'json',
  18. root: 'users'
  19. }
  20. }
  21. });
  22. var userStore;
  23. Ext.require('Ext.data.Store');
  24. Ext.onReady(function() {
  25. // Uses the User Model's Proxy
  26. userStore = Ext.create('Ext.data.Store', {
  27. model: 'User',
  28. autoLoad: true
  29. });
  30. // Gives us a reference to the User class
  31. var User = Ext.ModelMgr.getModel('User');
  32. var ed = Ext.create('User', {
  33. name: 'Ed Spencer',
  34. age : 25
  35. });
  36. // We can save Ed directly without having to add him to a Store first because we
  37. // configured a RestProxy this will automatically send a POST request to the url data/users
  38. ed.save({
  39. success: function(ed) {
  40. console.log("Saved Ed! His ID is "+ ed.getId());
  41. }
  42. });
  43. // Load User 1 and do something with it (performs a GET request to /users/1)
  44. User.load(1, {
  45. success: function(user) {
  46. console.log("Loaded user 1: " + user.get('name'));
  47. }
  48. });
  49. });