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