Association.html
10.1 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-data-association-Association'>/**
</span> * @author Ed Spencer
*
* Associations enable you to express relationships between different {@link Ext.data.Model Models}. Let's say we're
* writing an ecommerce system where Users can make Orders - there's a relationship between these Models that we can
* express like this:
*
* Ext.define('User', {
* extend: 'Ext.data.Model',
* fields: ['id', 'name', 'email'],
*
* hasMany: {model: 'Order', name: 'orders'}
* });
*
* Ext.define('Order', {
* extend: 'Ext.data.Model',
* fields: ['id', 'user_id', 'status', 'price'],
*
* belongsTo: 'User'
* });
*
* We've set up two models - User and Order - and told them about each other. You can set up as many associations on
* each Model as you need using the two default types - {@link Ext.data.HasManyAssociation hasMany} and {@link
* Ext.data.BelongsToAssociation belongsTo}. There's much more detail on the usage of each of those inside their
* documentation pages. If you're not familiar with Models already, {@link Ext.data.Model there is plenty on those too}.
*
* **Further Reading**
*
* - {@link Ext.data.association.HasMany hasMany associations}
* - {@link Ext.data.association.BelongsTo belongsTo associations}
* - {@link Ext.data.association.HasOne hasOne associations}
* - {@link Ext.data.Model using Models}
*
* # Self association models
*
* We can also have models that create parent/child associations between the same type. Below is an example, where
* groups can be nested inside other groups:
*
* // Server Data
* {
* "groups": {
* "id": 10,
* "parent_id": 100,
* "name": "Main Group",
* "parent_group": {
* "id": 100,
* "parent_id": null,
* "name": "Parent Group"
* },
* "child_groups": [{
* "id": 2,
* "parent_id": 10,
* "name": "Child Group 1"
* },{
* "id": 3,
* "parent_id": 10,
* "name": "Child Group 2"
* },{
* "id": 4,
* "parent_id": 10,
* "name": "Child Group 3"
* }]
* }
* }
*
* // Client code
* Ext.define('Group', {
* extend: 'Ext.data.Model',
* fields: ['id', 'parent_id', 'name'],
* proxy: {
* type: 'ajax',
* url: 'data.json',
* reader: {
* type: 'json',
* root: 'groups'
* }
* },
* associations: [{
* type: 'hasMany',
* model: 'Group',
* primaryKey: 'id',
* foreignKey: 'parent_id',
* autoLoad: true,
* associationKey: 'child_groups' // read child data from child_groups
* }, {
* type: 'belongsTo',
* model: 'Group',
* primaryKey: 'id',
* foreignKey: 'parent_id',
* associationKey: 'parent_group' // read parent data from parent_group
* }]
* });
*
* Ext.onReady(function(){
*
* Group.load(10, {
* success: function(group){
* console.log(group.getGroup().get('name'));
*
* group.groups().each(function(rec){
* console.log(rec.get('name'));
* });
* }
* });
*
* });
*
*/
Ext.define('Ext.data.association.Association', {
alternateClassName: 'Ext.data.Association',
<span id='Ext-data-association-Association-cfg-ownerModel'> /**
</span> * @cfg {String} ownerModel
* The string name of the model that owns the association.
*
* **NB!** This config is required when instantiating the Association directly.
* However, it cannot be used at all when defining the association as a config
* object inside Model, because the name of the model itself will be supplied
* automatically as the value of this config.
*/
<span id='Ext-data-association-Association-cfg-associatedModel'> /**
</span> * @cfg {String} associatedModel
* The string name of the model that is being associated with.
*
* **NB!** This config is required when instantiating the Association directly.
* When defining the association as a config object inside Model, the #model
* configuration will shadow this config.
*/
<span id='Ext-data-association-Association-cfg-model'> /**
</span> * @cfg {String} model
* The string name of the model that is being associated with.
*
* This config option is to be used when defining the association as a config
* object within Model. The value is then mapped to #associatedModel when
* Association is instantiated inside Model.
*/
<span id='Ext-data-association-Association-cfg-primaryKey'> /**
</span> * @cfg {String} primaryKey
* The name of the primary key on the associated model. In general this will be the
* {@link Ext.data.Model#idProperty} of the Model.
*/
primaryKey: 'id',
<span id='Ext-data-association-Association-cfg-reader'> /**
</span> * @cfg {Ext.data.reader.Reader} reader
* A special reader to read associated data
*/
<span id='Ext-data-association-Association-cfg-associationKey'> /**
</span> * @cfg {String} associationKey
* The name of the property in the data to read the association from. Defaults to the name of the associated model.
*/
defaultReaderType: 'json',
isAssociation: true,
initialConfig: null,
statics: {
AUTO_ID: 1000,
create: function(association){
if (Ext.isString(association)) {
association = {
type: association
};
}
switch (association.type) {
case 'belongsTo':
return new Ext.data.association.BelongsTo(association);
case 'hasMany':
return new Ext.data.association.HasMany(association);
case 'hasOne':
return new Ext.data.association.HasOne(association);
//TODO Add this back when it's fixed
// case 'polymorphic':
// return Ext.create('Ext.data.PolymorphicAssociation', association);
default:
//<debug>
Ext.Error.raise('Unknown Association type: "' + association.type + '"');
//</debug>
}
return association;
}
},
<span id='Ext-data-association-Association-method-constructor'> /**
</span> * Creates the Association object.
* @param {Object} [config] Config object.
*/
constructor: function(config) {
Ext.apply(this, config);
var me = this,
types = Ext.ModelManager.types,
ownerName = config.ownerModel,
associatedName = config.associatedModel,
ownerModel = types[ownerName],
associatedModel = types[associatedName];
me.initialConfig = config;
//<debug>
if (ownerModel === undefined) {
Ext.Error.raise("The configured ownerModel was not valid (you tried " + ownerName + ")");
}
if (associatedModel === undefined) {
Ext.Error.raise("The configured associatedModel was not valid (you tried " + associatedName + ")");
}
//</debug>
me.ownerModel = ownerModel;
me.associatedModel = associatedModel;
<span id='Ext-data-association-Association-property-ownerName'> /**
</span> * @property {String} ownerName
* The name of the model that 'owns' the association
*/
<span id='Ext-data-association-Association-property-associatedName'> /**
</span> * @property {String} associatedName
* The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is
* 'Order')
*/
Ext.applyIf(me, {
ownerName : ownerName,
associatedName: associatedName
});
me.associationId = 'association' + (++me.statics().AUTO_ID);
},
<span id='Ext-data-association-Association-method-getReader'> /**
</span> * Get a specialized reader for reading associated data
* @return {Ext.data.reader.Reader} The reader, null if not supplied
*/
getReader: function(){
var me = this,
reader = me.reader,
model = me.associatedModel;
if (reader) {
if (Ext.isString(reader)) {
reader = {
type: reader
};
}
if (reader.isReader) {
reader.setModel(model);
} else {
Ext.applyIf(reader, {
model: model,
type : me.defaultReaderType
});
}
me.reader = Ext.createByAlias('reader.' + reader.type, reader);
}
return me.reader || null;
}
});
</pre>
</body>
</html>