4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-Association'>/**
19 </span> * @author Ed Spencer
20 * @class Ext.data.Association
23 * <p>Associations enable you to express relationships between different {@link Ext.data.Model Models}. Let's say we're
24 * writing an ecommerce system where Users can make Orders - there's a relationship between these Models that we can
25 * express like this:</p>
27 <pre><code>
29 extend: 'Ext.data.Model',
30 fields: ['id', 'name', 'email'],
32 hasMany: {model: 'Order', name: 'orders'}
36 extend: 'Ext.data.Model',
37 fields: ['id', 'user_id', 'status', 'price'],
41 </code></pre>
43 * <p>We've set up two models - User and Order - and told them about each other. You can set up as many associations on
44 * each Model as you need using the two default types - {@link Ext.data.HasManyAssociation hasMany} and
45 * {@link Ext.data.BelongsToAssociation belongsTo}. There's much more detail on the usage of each of those inside their
46 * documentation pages. If you're not familiar with Models already, {@link Ext.data.Model there is plenty on those too}.</p>
48 * <p><u>Further Reading</u></p>
50 * <ul style="list-style-type: disc; padding-left: 20px;">
51 * <li>{@link Ext.data.HasManyAssociation hasMany associations}
52 * <li>{@link Ext.data.BelongsToAssociation belongsTo associations}
53 * <li>{@link Ext.data.Model using Models}
56 * <b>Self association models</b>
57 * <p>We can also have models that create parent/child associations between the same type. Below is an example, where
58 * groups can be nested inside other groups:</p>
59 * <pre><code>
65 "parent_id": 100,
66 "name": "Main Group",
67 "parent_group": {
69 "parent_id": null,
70 "name": "Parent Group"
72 "child_groups": [{
74 "parent_id": 10,
75 "name": "Child Group 1"
78 "parent_id": 10,
79 "name": "Child Group 2"
82 "parent_id": 10,
83 "name": "Child Group 3"
90 extend: 'Ext.data.Model',
91 fields: ['id', 'parent_id', 'name'],
104 foreignKey: 'parent_id',
106 associationKey: 'child_groups' // read child data from child_groups
111 foreignKey: 'parent_id',
113 associationKey: 'parent_group' // read parent data from parent_group
118 Ext.onReady(function(){
121 success: function(group){
122 console.log(group.getGroup().get('name'));
124 group.groups().each(function(rec){
125 console.log(rec.get('name'));
131 * </code></pre>
134 Ext.define('Ext.data.Association', {
135 <span id='Ext-data-Association-cfg-ownerModel'> /**
136 </span> * @cfg {String} ownerModel The string name of the model that owns the association. Required
139 <span id='Ext-data-Association-cfg-associatedModel'> /**
140 </span> * @cfg {String} associatedModel The string name of the model that is being associated with. Required
143 <span id='Ext-data-Association-cfg-primaryKey'> /**
144 </span> * @cfg {String} primaryKey The name of the primary key on the associated model. Defaults to 'id'.
145 * In general this will be the {@link Ext.data.Model#idProperty} of the Model.
149 <span id='Ext-data-Association-cfg-reader'> /**
150 </span> * @cfg {Ext.data.reader.Reader} reader A special reader to read associated data
153 <span id='Ext-data-Association-cfg-associationKey'> /**
154 </span> * @cfg {String} associationKey The name of the property in the data to read the association from.
155 * Defaults to the name of the associated model.
158 defaultReaderType: 'json',
161 create: function(association){
162 if (!association.isAssociation) {
163 if (Ext.isString(association)) {
169 switch (association.type) {
171 return Ext.create('Ext.data.BelongsToAssociation', association);
173 return Ext.create('Ext.data.HasManyAssociation', association);
174 //TODO Add this back when it's fixed
175 // case 'polymorphic':
176 // return Ext.create('Ext.data.PolymorphicAssociation', association);
179 Ext.Error.raise('Unknown Association type: "' + association.type + '"');
187 <span id='Ext-data-Association-method-constructor'> /**
188 </span> * Creates the Association object.
189 * @param {Object} config (optional) Config object.
191 constructor: function(config) {
192 Ext.apply(this, config);
194 var types = Ext.ModelManager.types,
195 ownerName = config.ownerModel,
196 associatedName = config.associatedModel,
197 ownerModel = types[ownerName],
198 associatedModel = types[associatedName],
202 if (ownerModel === undefined) {
203 Ext.Error.raise("The configured ownerModel was not valid (you tried " + ownerName + ")");
205 if (associatedModel === undefined) {
206 Ext.Error.raise("The configured associatedModel was not valid (you tried " + associatedName + ")");
210 this.ownerModel = ownerModel;
211 this.associatedModel = associatedModel;
213 <span id='Ext-data-Association-property-ownerName'> /**
214 </span> * The name of the model that 'owns' the association
215 * @property ownerName
219 <span id='Ext-data-Association-property-associatedName'> /**
220 </span> * The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is 'Order')
221 * @property associatedName
226 ownerName : ownerName,
227 associatedName: associatedName
231 <span id='Ext-data-Association-method-getReader'> /**
232 </span> * Get a specialized reader for reading associated data
233 * @return {Ext.data.reader.Reader} The reader, null if not supplied
235 getReader: function(){
238 model = me.associatedModel;
241 if (Ext.isString(reader)) {
246 if (reader.isReader) {
247 reader.setModel(model);
249 Ext.applyIf(reader, {
251 type : me.defaultReaderType
254 me.reader = Ext.createByAlias('reader.' + reader.type, reader);
256 return me.reader || null;