1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.Association-method-constructor'><span id='Ext-data.Association'>/**
2 </span></span> * @author Ed Spencer
3 * @class Ext.data.Association
6 * <p>Associations enable you to express relationships between different {@link Ext.data.Model Models}. Let's say we're
7 * writing an ecommerce system where Users can make Orders - there's a relationship between these Models that we can
8 * express like this:</p>
10 <pre><code>
12 extend: 'Ext.data.Model',
13 fields: ['id', 'name', 'email'],
15 hasMany: {model: 'Order', name: 'orders'}
19 extend: 'Ext.data.Model',
20 fields: ['id', 'user_id', 'status', 'price'],
24 </code></pre>
26 * <p>We've set up two models - User and Order - and told them about each other. You can set up as many associations on
27 * each Model as you need using the two default types - {@link Ext.data.HasManyAssociation hasMany} and
28 * {@link Ext.data.BelongsToAssociation belongsTo}. There's much more detail on the usage of each of those inside their
29 * documentation pages. If you're not familiar with Models already, {@link Ext.data.Model there is plenty on those too}.</p>
31 * <p><u>Further Reading</u></p>
33 * <ul style="list-style-type: disc; padding-left: 20px;">
34 * <li>{@link Ext.data.HasManyAssociation hasMany associations}
35 * <li>{@link Ext.data.BelongsToAssociation belongsTo associations}
36 * <li>{@link Ext.data.Model using Models}
39 * <b>Self association models</b>
40 * <p>We can also have models that create parent/child associations between the same type. Below is an example, where
41 * groups can be nested inside other groups:</p>
42 * <pre><code>
48 "parent_id": 100,
49 "name": "Main Group",
50 "parent_group": {
52 "parent_id": null,
53 "name": "Parent Group"
55 "child_groups": [{
57 "parent_id": 10,
58 "name": "Child Group 1"
61 "parent_id": 10,
62 "name": "Child Group 2"
65 "parent_id": 10,
66 "name": "Child Group 3"
73 extend: 'Ext.data.Model',
74 fields: ['id', 'parent_id', 'name'],
87 foreignKey: 'parent_id',
89 associationKey: 'child_groups' // read child data from child_groups
94 foreignKey: 'parent_id',
96 associationKey: 'parent_group' // read parent data from parent_group
101 Ext.onReady(function(){
104 success: function(group){
105 console.log(group.getGroup().get('name'));
107 group.groups().each(function(rec){
108 console.log(rec.get('name'));
114 * </code></pre>
117 * @param {Object} config Optional config object
119 Ext.define('Ext.data.Association', {
120 <span id='Ext-data.Association-cfg-ownerModel'> /**
121 </span> * @cfg {String} ownerModel The string name of the model that owns the association. Required
124 <span id='Ext-data.Association-cfg-associatedModel'> /**
125 </span> * @cfg {String} associatedModel The string name of the model that is being associated with. Required
128 <span id='Ext-data.Association-cfg-primaryKey'> /**
129 </span> * @cfg {String} primaryKey The name of the primary key on the associated model. Defaults to 'id'.
130 * In general this will be the {@link Ext.data.Model#idProperty} of the Model.
134 <span id='Ext-data.Association-cfg-reader'> /**
135 </span> * @cfg {Ext.data.reader.Reader} reader A special reader to read associated data
138 <span id='Ext-data.Association-cfg-associationKey'> /**
139 </span> * @cfg {String} associationKey The name of the property in the data to read the association from.
140 * Defaults to the name of the associated model.
143 defaultReaderType: 'json',
146 create: function(association){
147 if (!association.isAssociation) {
148 if (Ext.isString(association)) {
154 switch (association.type) {
156 return Ext.create('Ext.data.BelongsToAssociation', association);
158 return Ext.create('Ext.data.HasManyAssociation', association);
159 //TODO Add this back when it's fixed
160 // case 'polymorphic':
161 // return Ext.create('Ext.data.PolymorphicAssociation', association);
164 Ext.Error.raise('Unknown Association type: "' + association.type + '"');
172 constructor: function(config) {
173 Ext.apply(this, config);
175 var types = Ext.ModelManager.types,
176 ownerName = config.ownerModel,
177 associatedName = config.associatedModel,
178 ownerModel = types[ownerName],
179 associatedModel = types[associatedName],
183 if (ownerModel === undefined) {
184 Ext.Error.raise("The configured ownerModel was not valid (you tried " + ownerName + ")");
186 if (associatedModel === undefined) {
187 Ext.Error.raise("The configured associatedModel was not valid (you tried " + associatedName + ")");
191 this.ownerModel = ownerModel;
192 this.associatedModel = associatedModel;
194 <span id='Ext-data.Association-property-ownerName'> /**
195 </span> * The name of the model that 'owns' the association
196 * @property ownerName
200 <span id='Ext-data.Association-property-associatedName'> /**
201 </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')
202 * @property associatedName
207 ownerName : ownerName,
208 associatedName: associatedName
212 <span id='Ext-data.Association-method-getReader'> /**
213 </span> * Get a specialized reader for reading associated data
214 * @return {Ext.data.reader.Reader} The reader, null if not supplied
216 getReader: function(){
219 model = me.associatedModel;
222 if (Ext.isString(reader)) {
227 if (reader.isReader) {
228 reader.setModel(model);
230 Ext.applyIf(reader, {
232 type : me.defaultReaderType
235 me.reader = Ext.createByAlias('reader.' + reader.type, reader);
237 return me.reader || null;
240 </pre></pre></body></html>