Association.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  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; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-data-association-Association'>/**
  19. </span> * @author Ed Spencer
  20. *
  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
  23. * express like this:
  24. *
  25. * Ext.define('User', {
  26. * extend: 'Ext.data.Model',
  27. * fields: ['id', 'name', 'email'],
  28. *
  29. * hasMany: {model: 'Order', name: 'orders'}
  30. * });
  31. *
  32. * Ext.define('Order', {
  33. * extend: 'Ext.data.Model',
  34. * fields: ['id', 'user_id', 'status', 'price'],
  35. *
  36. * belongsTo: 'User'
  37. * });
  38. *
  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}.
  43. *
  44. * **Further Reading**
  45. *
  46. * - {@link Ext.data.association.HasMany hasMany associations}
  47. * - {@link Ext.data.association.BelongsTo belongsTo associations}
  48. * - {@link Ext.data.association.HasOne hasOne associations}
  49. * - {@link Ext.data.Model using Models}
  50. *
  51. * # Self association models
  52. *
  53. * We can also have models that create parent/child associations between the same type. Below is an example, where
  54. * groups can be nested inside other groups:
  55. *
  56. * // Server Data
  57. * {
  58. * &quot;groups&quot;: {
  59. * &quot;id&quot;: 10,
  60. * &quot;parent_id&quot;: 100,
  61. * &quot;name&quot;: &quot;Main Group&quot;,
  62. * &quot;parent_group&quot;: {
  63. * &quot;id&quot;: 100,
  64. * &quot;parent_id&quot;: null,
  65. * &quot;name&quot;: &quot;Parent Group&quot;
  66. * },
  67. * &quot;child_groups&quot;: [{
  68. * &quot;id&quot;: 2,
  69. * &quot;parent_id&quot;: 10,
  70. * &quot;name&quot;: &quot;Child Group 1&quot;
  71. * },{
  72. * &quot;id&quot;: 3,
  73. * &quot;parent_id&quot;: 10,
  74. * &quot;name&quot;: &quot;Child Group 2&quot;
  75. * },{
  76. * &quot;id&quot;: 4,
  77. * &quot;parent_id&quot;: 10,
  78. * &quot;name&quot;: &quot;Child Group 3&quot;
  79. * }]
  80. * }
  81. * }
  82. *
  83. * // Client code
  84. * Ext.define('Group', {
  85. * extend: 'Ext.data.Model',
  86. * fields: ['id', 'parent_id', 'name'],
  87. * proxy: {
  88. * type: 'ajax',
  89. * url: 'data.json',
  90. * reader: {
  91. * type: 'json',
  92. * root: 'groups'
  93. * }
  94. * },
  95. * associations: [{
  96. * type: 'hasMany',
  97. * model: 'Group',
  98. * primaryKey: 'id',
  99. * foreignKey: 'parent_id',
  100. * autoLoad: true,
  101. * associationKey: 'child_groups' // read child data from child_groups
  102. * }, {
  103. * type: 'belongsTo',
  104. * model: 'Group',
  105. * primaryKey: 'id',
  106. * foreignKey: 'parent_id',
  107. * associationKey: 'parent_group' // read parent data from parent_group
  108. * }]
  109. * });
  110. *
  111. * Ext.onReady(function(){
  112. *
  113. * Group.load(10, {
  114. * success: function(group){
  115. * console.log(group.getGroup().get('name'));
  116. *
  117. * group.groups().each(function(rec){
  118. * console.log(rec.get('name'));
  119. * });
  120. * }
  121. * });
  122. *
  123. * });
  124. *
  125. */
  126. Ext.define('Ext.data.association.Association', {
  127. alternateClassName: 'Ext.data.Association',
  128. <span id='Ext-data-association-Association-cfg-ownerModel'> /**
  129. </span> * @cfg {String} ownerModel
  130. * The string name of the model that owns the association.
  131. *
  132. * **NB!** This config is required when instantiating the Association directly.
  133. * However, it cannot be used at all when defining the association as a config
  134. * object inside Model, because the name of the model itself will be supplied
  135. * automatically as the value of this config.
  136. */
  137. <span id='Ext-data-association-Association-cfg-associatedModel'> /**
  138. </span> * @cfg {String} associatedModel
  139. * The string name of the model that is being associated with.
  140. *
  141. * **NB!** This config is required when instantiating the Association directly.
  142. * When defining the association as a config object inside Model, the #model
  143. * configuration will shadow this config.
  144. */
  145. <span id='Ext-data-association-Association-cfg-model'> /**
  146. </span> * @cfg {String} model
  147. * The string name of the model that is being associated with.
  148. *
  149. * This config option is to be used when defining the association as a config
  150. * object within Model. The value is then mapped to #associatedModel when
  151. * Association is instantiated inside Model.
  152. */
  153. <span id='Ext-data-association-Association-cfg-primaryKey'> /**
  154. </span> * @cfg {String} primaryKey
  155. * The name of the primary key on the associated model. In general this will be the
  156. * {@link Ext.data.Model#idProperty} of the Model.
  157. */
  158. primaryKey: 'id',
  159. <span id='Ext-data-association-Association-cfg-reader'> /**
  160. </span> * @cfg {Ext.data.reader.Reader} reader
  161. * A special reader to read associated data
  162. */
  163. <span id='Ext-data-association-Association-cfg-associationKey'> /**
  164. </span> * @cfg {String} associationKey
  165. * The name of the property in the data to read the association from. Defaults to the name of the associated model.
  166. */
  167. defaultReaderType: 'json',
  168. isAssociation: true,
  169. initialConfig: null,
  170. statics: {
  171. AUTO_ID: 1000,
  172. create: function(association){
  173. if (Ext.isString(association)) {
  174. association = {
  175. type: association
  176. };
  177. }
  178. switch (association.type) {
  179. case 'belongsTo':
  180. return new Ext.data.association.BelongsTo(association);
  181. case 'hasMany':
  182. return new Ext.data.association.HasMany(association);
  183. case 'hasOne':
  184. return new Ext.data.association.HasOne(association);
  185. //TODO Add this back when it's fixed
  186. // case 'polymorphic':
  187. // return Ext.create('Ext.data.PolymorphicAssociation', association);
  188. default:
  189. //&lt;debug&gt;
  190. Ext.Error.raise('Unknown Association type: &quot;' + association.type + '&quot;');
  191. //&lt;/debug&gt;
  192. }
  193. return association;
  194. }
  195. },
  196. <span id='Ext-data-association-Association-method-constructor'> /**
  197. </span> * Creates the Association object.
  198. * @param {Object} [config] Config object.
  199. */
  200. constructor: function(config) {
  201. Ext.apply(this, config);
  202. var me = this,
  203. types = Ext.ModelManager.types,
  204. ownerName = config.ownerModel,
  205. associatedName = config.associatedModel,
  206. ownerModel = types[ownerName],
  207. associatedModel = types[associatedName];
  208. me.initialConfig = config;
  209. //&lt;debug&gt;
  210. if (ownerModel === undefined) {
  211. Ext.Error.raise(&quot;The configured ownerModel was not valid (you tried &quot; + ownerName + &quot;)&quot;);
  212. }
  213. if (associatedModel === undefined) {
  214. Ext.Error.raise(&quot;The configured associatedModel was not valid (you tried &quot; + associatedName + &quot;)&quot;);
  215. }
  216. //&lt;/debug&gt;
  217. me.ownerModel = ownerModel;
  218. me.associatedModel = associatedModel;
  219. <span id='Ext-data-association-Association-property-ownerName'> /**
  220. </span> * @property {String} ownerName
  221. * The name of the model that 'owns' the association
  222. */
  223. <span id='Ext-data-association-Association-property-associatedName'> /**
  224. </span> * @property {String} associatedName
  225. * The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is
  226. * 'Order')
  227. */
  228. Ext.applyIf(me, {
  229. ownerName : ownerName,
  230. associatedName: associatedName
  231. });
  232. me.associationId = 'association' + (++me.statics().AUTO_ID);
  233. },
  234. <span id='Ext-data-association-Association-method-getReader'> /**
  235. </span> * Get a specialized reader for reading associated data
  236. * @return {Ext.data.reader.Reader} The reader, null if not supplied
  237. */
  238. getReader: function(){
  239. var me = this,
  240. reader = me.reader,
  241. model = me.associatedModel;
  242. if (reader) {
  243. if (Ext.isString(reader)) {
  244. reader = {
  245. type: reader
  246. };
  247. }
  248. if (reader.isReader) {
  249. reader.setModel(model);
  250. } else {
  251. Ext.applyIf(reader, {
  252. model: model,
  253. type : me.defaultReaderType
  254. });
  255. }
  256. me.reader = Ext.createByAlias('reader.' + reader.type, reader);
  257. }
  258. return me.reader || null;
  259. }
  260. });
  261. </pre>
  262. </body>
  263. </html>