3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
17 * @class Ext.data.Association
20 * <p>Associations enable you to express relationships between different {@link Ext.data.Model Models}. Let's say we're
21 * writing an ecommerce system where Users can make Orders - there's a relationship between these Models that we can
22 * express like this:</p>
26 extend: 'Ext.data.Model',
27 fields: ['id', 'name', 'email'],
29 hasMany: {model: 'Order', name: 'orders'}
33 extend: 'Ext.data.Model',
34 fields: ['id', 'user_id', 'status', 'price'],
40 * <p>We've set up two models - User and Order - and told them about each other. You can set up as many associations on
41 * each Model as you need using the two default types - {@link Ext.data.HasManyAssociation hasMany} and
42 * {@link Ext.data.BelongsToAssociation belongsTo}. There's much more detail on the usage of each of those inside their
43 * documentation pages. If you're not familiar with Models already, {@link Ext.data.Model there is plenty on those too}.</p>
45 * <p><u>Further Reading</u></p>
47 * <ul style="list-style-type: disc; padding-left: 20px;">
48 * <li>{@link Ext.data.HasManyAssociation hasMany associations}
49 * <li>{@link Ext.data.BelongsToAssociation belongsTo associations}
50 * <li>{@link Ext.data.Model using Models}
53 * <b>Self association models</b>
54 * <p>We can also have models that create parent/child associations between the same type. Below is an example, where
55 * groups can be nested inside other groups:</p>
67 "name": "Parent Group"
72 "name": "Child Group 1"
76 "name": "Child Group 2"
80 "name": "Child Group 3"
87 extend: 'Ext.data.Model',
88 fields: ['id', 'parent_id', 'name'],
101 foreignKey: 'parent_id',
103 associationKey: 'child_groups' // read child data from child_groups
108 foreignKey: 'parent_id',
110 associationKey: 'parent_group' // read parent data from parent_group
115 Ext.onReady(function(){
118 success: function(group){
119 console.log(group.getGroup().get('name'));
121 group.groups().each(function(rec){
122 console.log(rec.get('name'));
131 Ext.define('Ext.data.Association', {
133 * @cfg {String} ownerModel The string name of the model that owns the association. Required
137 * @cfg {String} associatedModel The string name of the model that is being associated with. Required
141 * @cfg {String} primaryKey The name of the primary key on the associated model. Defaults to 'id'.
142 * In general this will be the {@link Ext.data.Model#idProperty} of the Model.
147 * @cfg {Ext.data.reader.Reader} reader A special reader to read associated data
151 * @cfg {String} associationKey The name of the property in the data to read the association from.
152 * Defaults to the name of the associated model.
155 defaultReaderType: 'json',
158 create: function(association){
159 if (!association.isAssociation) {
160 if (Ext.isString(association)) {
166 switch (association.type) {
168 return Ext.create('Ext.data.BelongsToAssociation', association);
170 return Ext.create('Ext.data.HasManyAssociation', association);
171 //TODO Add this back when it's fixed
172 // case 'polymorphic':
173 // return Ext.create('Ext.data.PolymorphicAssociation', association);
176 Ext.Error.raise('Unknown Association type: "' + association.type + '"');
185 * Creates the Association object.
186 * @param {Object} config (optional) Config object.
188 constructor: function(config) {
189 Ext.apply(this, config);
191 var types = Ext.ModelManager.types,
192 ownerName = config.ownerModel,
193 associatedName = config.associatedModel,
194 ownerModel = types[ownerName],
195 associatedModel = types[associatedName],
199 if (ownerModel === undefined) {
200 Ext.Error.raise("The configured ownerModel was not valid (you tried " + ownerName + ")");
202 if (associatedModel === undefined) {
203 Ext.Error.raise("The configured associatedModel was not valid (you tried " + associatedName + ")");
207 this.ownerModel = ownerModel;
208 this.associatedModel = associatedModel;
211 * The name of the model that 'owns' the association
212 * @property ownerName
217 * The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is 'Order')
218 * @property associatedName
223 ownerName : ownerName,
224 associatedName: associatedName
229 * Get a specialized reader for reading associated data
230 * @return {Ext.data.reader.Reader} The reader, null if not supplied
232 getReader: function(){
235 model = me.associatedModel;
238 if (Ext.isString(reader)) {
243 if (reader.isReader) {
244 reader.setModel(model);
246 Ext.applyIf(reader, {
248 type : me.defaultReaderType
251 me.reader = Ext.createByAlias('reader.' + reader.type, reader);
253 return me.reader || null;