app.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @example Associations and Validations
  3. *
  4. * This example demonstrates associations and validations on a {@link Ext.data.Model}.
  5. * See console for output.
  6. */
  7. // define the User model
  8. Ext.define('User', {
  9. extend: 'Ext.data.Model',
  10. fields: ['id', 'name', 'age', 'gender'],
  11. validations: [
  12. {type: 'presence', name: 'name'},
  13. {type: 'length', name: 'name', min: 5},
  14. {type: 'format', name: 'age', matcher: /\d+/},
  15. {type: 'inclusion', name: 'gender', list: ['male', 'female']},
  16. {type: 'exclusion', name: 'name', list: ['admin']}
  17. ],
  18. proxy: {
  19. type: 'rest',
  20. url : 'data/users',
  21. reader: {
  22. type: 'json',
  23. root: 'users'
  24. }
  25. },
  26. hasMany: 'Post' // shorthand for {model: 'Post', name: 'posts'}
  27. });
  28. //define the Post model
  29. Ext.define('Post', {
  30. extend: 'Ext.data.Model',
  31. fields: ['id', 'user_id', 'title', 'body'],
  32. proxy: {
  33. type: 'rest',
  34. url : 'data/posts',
  35. reader: {
  36. type: 'json',
  37. root: 'posts'
  38. }
  39. },
  40. belongsTo: 'User',
  41. hasMany: {model: 'Comment', name: 'comments'}
  42. });
  43. //define the Comment model
  44. Ext.define('Comment', {
  45. extend: 'Ext.data.Model',
  46. fields: ['id', 'post_id', 'name', 'message'],
  47. belongsTo: 'Post'
  48. });
  49. Ext.require('Ext.data.Store');
  50. Ext.onReady(function() {
  51. // Loads User with ID 1 and related posts and comments using User's Proxy
  52. User.load(1, {
  53. success: function(user) {
  54. console.log("User: " + user.get('name'));
  55. // loop through the user's posts and print out the comments
  56. user.posts().each(function(post) {
  57. console.log("Comments for post: " + post.get('title'));
  58. post.comments().each(function(comment) {
  59. console.log(comment.get('message'));
  60. });
  61. // get the user reference from the post's belongsTo association
  62. post.getUser(function(user) {
  63. console.log('Just got the user reference from the post: ' + user.get('name'))
  64. });
  65. // try to change the post's user
  66. post.setUser(100, {
  67. callback: function(product, operation) {
  68. if (operation.wasSuccessful()) {
  69. console.log('Post\'s user was updated');
  70. } else {
  71. console.log('Post\'s user could not be updated');
  72. }
  73. }
  74. });
  75. });
  76. // create a new post
  77. user.posts().add({
  78. title: 'Ext JS 4.0 MVC Architecture',
  79. body: 'It\'s a great Idea to structure your Ext JS Applications using the built in MVC Architecture...'
  80. });
  81. // save the new post
  82. user.posts().sync();
  83. }
  84. });
  85. // now lets try to create a new user with as many validation errors as we can
  86. var newUser = Ext.create('User', {
  87. name: 'admin',
  88. age: 'twenty-nine',
  89. gender: 'not a valid gender'
  90. });
  91. // run some validation on the new user we just created
  92. var errors = newUser.validate();
  93. console.log('Is User valid?', errors.isValid()); //returns 'false' as there were validation errors
  94. console.log('All Errors:', errors.items); //returns the array of all errors found on this model instance
  95. console.log('Age Errors:', errors.getByField('age')); //returns the errors for the age field
  96. });