4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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
21 * Associations enable you to express relationships between different {@link Ext.data.Model Models}. Let's say we're
22 * writing an ecommerce system where Users can make Orders - there's a relationship between these Models that we can
25 * Ext.define('User', {
26 * extend: 'Ext.data.Model',
27 * fields: ['id', 'name', 'email'],
29 * hasMany: {model: 'Order', name: 'orders'}
32 * Ext.define('Order', {
33 * extend: 'Ext.data.Model',
34 * fields: ['id', 'user_id', 'status', 'price'],
39 * We've set up two models - User and Order - and told them about each other. You can set up as many associations on
40 * each Model as you need using the two default types - {@link Ext.data.HasManyAssociation hasMany} and {@link
41 * Ext.data.BelongsToAssociation belongsTo}. There's much more detail on the usage of each of those inside their
42 * documentation pages. If you're not familiar with Models already, {@link Ext.data.Model there is plenty on those too}.
46 * - {@link Ext.data.HasManyAssociation hasMany associations}
47 * - {@link Ext.data.BelongsToAssociation belongsTo associations}
48 * - {@link Ext.data.Model using Models}
50 * # Self association models
52 * We can also have models that create parent/child associations between the same type. Below is an example, where
53 * groups can be nested inside other groups:
57 * "groups": {
59 * "parent_id": 100,
60 * "name": "Main Group",
61 * "parent_group": {
62 * "id": 100,
63 * "parent_id": null,
64 * "name": "Parent Group"
66 * "child_groups": [{
68 * "parent_id": 10,
69 * "name": "Child Group 1"
72 * "parent_id": 10,
73 * "name": "Child Group 2"
76 * "parent_id": 10,
77 * "name": "Child Group 3"
83 * Ext.define('Group', {
84 * extend: 'Ext.data.Model',
85 * fields: ['id', 'parent_id', 'name'],
98 * foreignKey: 'parent_id',
100 * associationKey: 'child_groups' // read child data from child_groups
105 * foreignKey: 'parent_id',
106 * associationKey: 'parent_group' // read parent data from parent_group
110 * Ext.onReady(function(){
113 * success: function(group){
114 * console.log(group.getGroup().get('name'));
116 * group.groups().each(function(rec){
117 * console.log(rec.get('name'));
125 Ext.define('Ext.data.Association', {
126 <span id='Ext-data-Association-cfg-ownerModel'> /**
127 </span> * @cfg {String} ownerModel (required)
128 * The string name of the model that owns the association.
131 <span id='Ext-data-Association-cfg-associatedModel'> /**
132 </span> * @cfg {String} associatedModel (required)
133 * The string name of the model that is being associated with.
136 <span id='Ext-data-Association-cfg-primaryKey'> /**
137 </span> * @cfg {String} primaryKey
138 * The name of the primary key on the associated model. In general this will be the
139 * {@link Ext.data.Model#idProperty} of the Model.
143 <span id='Ext-data-Association-cfg-reader'> /**
144 </span> * @cfg {Ext.data.reader.Reader} reader
145 * A special reader to read associated data
148 <span id='Ext-data-Association-cfg-associationKey'> /**
149 </span> * @cfg {String} associationKey
150 * The name of the property in the data to read the association from. Defaults to the name of the associated model.
153 defaultReaderType: 'json',
156 create: function(association){
157 if (!association.isAssociation) {
158 if (Ext.isString(association)) {
164 switch (association.type) {
166 return Ext.create('Ext.data.BelongsToAssociation', association);
168 return Ext.create('Ext.data.HasManyAssociation', association);
169 //TODO Add this back when it's fixed
170 // case 'polymorphic':
171 // return Ext.create('Ext.data.PolymorphicAssociation', association);
174 Ext.Error.raise('Unknown Association type: "' + association.type + '"');
182 <span id='Ext-data-Association-method-constructor'> /**
183 </span> * Creates the Association object.
184 * @param {Object} [config] Config object.
186 constructor: function(config) {
187 Ext.apply(this, config);
189 var types = Ext.ModelManager.types,
190 ownerName = config.ownerModel,
191 associatedName = config.associatedModel,
192 ownerModel = types[ownerName],
193 associatedModel = types[associatedName],
197 if (ownerModel === undefined) {
198 Ext.Error.raise("The configured ownerModel was not valid (you tried " + ownerName + ")");
200 if (associatedModel === undefined) {
201 Ext.Error.raise("The configured associatedModel was not valid (you tried " + associatedName + ")");
205 this.ownerModel = ownerModel;
206 this.associatedModel = associatedModel;
208 <span id='Ext-data-Association-property-ownerName'> /**
209 </span> * @property {String} ownerName
210 * The name of the model that 'owns' the association
213 <span id='Ext-data-Association-property-associatedName'> /**
214 </span> * @property {String} associatedName
215 * The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is
220 ownerName : ownerName,
221 associatedName: associatedName
225 <span id='Ext-data-Association-method-getReader'> /**
226 </span> * Get a specialized reader for reading associated data
227 * @return {Ext.data.reader.Reader} The reader, null if not supplied
229 getReader: function(){
232 model = me.associatedModel;
235 if (Ext.isString(reader)) {
240 if (reader.isReader) {
241 reader.setModel(model);
243 Ext.applyIf(reader, {
245 type : me.defaultReaderType
248 me.reader = Ext.createByAlias('reader.' + reader.type, reader);
250 return me.reader || null;