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