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-method-constructor'><span id='Ext-data-Association'>/**
19 </span></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 * @param {Object} config Optional config object
136 Ext.define('Ext.data.Association', {
137 <span id='Ext-data-Association-cfg-ownerModel'> /**
138 </span> * @cfg {String} ownerModel The string name of the model that owns the association. Required
141 <span id='Ext-data-Association-cfg-associatedModel'> /**
142 </span> * @cfg {String} associatedModel The string name of the model that is being associated with. Required
145 <span id='Ext-data-Association-cfg-primaryKey'> /**
146 </span> * @cfg {String} primaryKey The name of the primary key on the associated model. Defaults to 'id'.
147 * In general this will be the {@link Ext.data.Model#idProperty} of the Model.
151 <span id='Ext-data-Association-cfg-reader'> /**
152 </span> * @cfg {Ext.data.reader.Reader} reader A special reader to read associated data
155 <span id='Ext-data-Association-cfg-associationKey'> /**
156 </span> * @cfg {String} associationKey The name of the property in the data to read the association from.
157 * Defaults to the name of the associated model.
160 defaultReaderType: 'json',
163 create: function(association){
164 if (!association.isAssociation) {
165 if (Ext.isString(association)) {
171 switch (association.type) {
173 return Ext.create('Ext.data.BelongsToAssociation', association);
175 return Ext.create('Ext.data.HasManyAssociation', association);
176 //TODO Add this back when it's fixed
177 // case 'polymorphic':
178 // return Ext.create('Ext.data.PolymorphicAssociation', association);
181 Ext.Error.raise('Unknown Association type: "' + association.type + '"');
189 constructor: function(config) {
190 Ext.apply(this, config);
192 var types = Ext.ModelManager.types,
193 ownerName = config.ownerModel,
194 associatedName = config.associatedModel,
195 ownerModel = types[ownerName],
196 associatedModel = types[associatedName],
200 if (ownerModel === undefined) {
201 Ext.Error.raise("The configured ownerModel was not valid (you tried " + ownerName + ")");
203 if (associatedModel === undefined) {
204 Ext.Error.raise("The configured associatedModel was not valid (you tried " + associatedName + ")");
208 this.ownerModel = ownerModel;
209 this.associatedModel = associatedModel;
211 <span id='Ext-data-Association-property-ownerName'> /**
212 </span> * The name of the model that 'owns' the association
213 * @property ownerName
217 <span id='Ext-data-Association-property-associatedName'> /**
218 </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')
219 * @property associatedName
224 ownerName : ownerName,
225 associatedName: associatedName
229 <span id='Ext-data-Association-method-getReader'> /**
230 </span> * Get a specialized reader for reading associated data
231 * @return {Ext.data.reader.Reader} The reader, null if not supplied
233 getReader: function(){
236 model = me.associatedModel;
239 if (Ext.isString(reader)) {
244 if (reader.isReader) {
245 reader.setModel(model);
247 Ext.applyIf(reader, {
249 type : me.defaultReaderType
252 me.reader = Ext.createByAlias('reader.' + reader.type, reader);
254 return me.reader || null;