Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / output / Ext.data.BelongsToAssociation.js
1 Ext.data.JsonP.Ext_data_BelongsToAssociation({
2   "tagname": "class",
3   "name": "Ext.data.BelongsToAssociation",
4   "doc": "<p>Represents a many to one association with another model. The owner model is expected to have\na foreign key which references the primary key of the associated model:</p>\n\n\n\n\n<pre><code>Ext.define('Category', {\n    extend: 'Ext.data.Model',\n    fields: [\n        {name: 'id',   type: 'int'},\n        {name: 'name', type: 'string'}\n    ]\n});\n\nExt.define('Product', {\n    extend: 'Ext.data.Model',\n    fields: [\n        {name: 'id',          type: 'int'},\n        {name: 'category_id', type: 'int'},\n        {name: 'name',        type: 'string'}\n    ],\n    // we can use the belongsTo shortcut on the model to create a belongsTo association\n    belongsTo: {type: 'belongsTo', model: 'Category'}\n});\n</code></pre>\n\n\n<p>In the example above we have created models for Products and Categories, and linked them together\nby saying that each Product belongs to a Category. This automatically links each Product to a Category\nbased on the Product's category_id, and provides new functions on the Product model:</p>\n\n\n\n\n<p><u>Generated getter function</u></p>\n\n\n\n\n<p>The first function that is added to the owner model is a getter function:</p>\n\n\n\n\n<pre><code>var product = new Product({\n    id: 100,\n    category_id: 20,\n    name: 'Sneakers'\n});\n\nproduct.getCategory(function(category, operation) {\n    //do something with the category object\n    alert(category.get('id')); //alerts 20\n}, this);\n</code></pre>\n\n\n\n\n<p>The getCategory function was created on the Product model when we defined the association. This uses the\nCategory's configured <a href=\"#/api/Ext.data.proxy.Proxy\" rel=\"Ext.data.proxy.Proxy\" class=\"docClass\">proxy</a> to load the Category asynchronously, calling the provided\ncallback when it has loaded.</p>\n\n\n\n\n<p>The new getCategory function will also accept an object containing success, failure and callback properties\n- callback will always be called, success will only be called if the associated model was loaded successfully\nand failure will only be called if the associatied model could not be loaded:</p>\n\n\n\n\n<pre><code>product.getCategory({\n    callback: function(category, operation) {}, //a function that will always be called\n    success : function(category, operation) {}, //a function that will only be called if the load succeeded\n    failure : function(category, operation) {}, //a function that will only be called if the load did not succeed\n    scope   : this //optionally pass in a scope object to execute the callbacks in\n});\n</code></pre>\n\n\n\n\n<p>In each case above the callbacks are called with two arguments - the associated model instance and the\n<a href=\"#/api/Ext.data.Operation\" rel=\"Ext.data.Operation\" class=\"docClass\">operation</a> object that was executed to load that instance. The Operation object is\nuseful when the instance could not be loaded.</p>\n\n\n\n\n<p><u>Generated setter function</u></p>\n\n\n\n\n<p>The second generated function sets the associated model instance - if only a single argument is passed to\nthe setter then the following two calls are identical:</p>\n\n\n\n\n<pre><code>//this call\nproduct.setCategory(10);\n\n//is equivalent to this call:\nproduct.set('category_id', 10);\n</code></pre>\n\n\n<p>If we pass in a second argument, the model will be automatically saved and the second argument passed to\nthe owner model's <a href=\"#/api/Ext.data.Model-method-save\" rel=\"Ext.data.Model-method-save\" class=\"docClass\">save</a> method:</p>\n\n\n<pre><code>product.setCategory(10, function(product, operation) {\n    //the product has been saved\n    alert(product.get('category_id')); //now alerts 10\n});\n\n//alternative syntax:\nproduct.setCategory(10, {\n    callback: function(product, operation), //a function that will always be called\n    success : function(product, operation), //a function that will only be called if the load succeeded\n    failure : function(product, operation), //a function that will only be called if the load did not succeed\n    scope   : this //optionally pass in a scope object to execute the callbacks in\n})\n</code></pre>\n\n\n\n\n<p><u>Customisation</u></p>\n\n\n\n\n<p>Associations reflect on the models they are linking to automatically set up properties such as the\n<a href=\"#/api/Ext.data.BelongsToAssociation-cfg-primaryKey\" rel=\"Ext.data.BelongsToAssociation-cfg-primaryKey\" class=\"docClass\">primaryKey</a> and <a href=\"#/api/Ext.data.BelongsToAssociation-cfg-foreignKey\" rel=\"Ext.data.BelongsToAssociation-cfg-foreignKey\" class=\"docClass\">foreignKey</a>. These can alternatively be specified:</p>\n\n\n\n\n<pre><code>Ext.define('Product', {\n    fields: [...],\n\n    associations: [\n        {type: 'belongsTo', model: 'Category', primaryKey: 'unique_id', foreignKey: 'cat_id'}\n    ]\n});\n </code></pre>\n\n\n\n\n<p>Here we replaced the default primary key (defaults to 'id') and foreign key (calculated as 'category_id')\nwith our own settings. Usually this will not be needed.</p>\n\n",
5   "extends": "Ext.data.Association",
6   "mixins": [
7
8   ],
9   "alternateClassNames": [
10
11   ],
12   "xtype": null,
13   "author": "Ed Spencer",
14   "docauthor": null,
15   "singleton": false,
16   "private": false,
17   "cfg": [
18     {
19       "tagname": "cfg",
20       "name": "associatedModel",
21       "member": "Ext.data.Association",
22       "type": "String",
23       "doc": "<p>The string name of the model that is being associated with. Required</p>\n",
24       "private": false,
25       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
26       "linenr": 124,
27       "html_filename": "Association.html",
28       "href": "Association.html#Ext-data-Association-cfg-associatedModel"
29     },
30     {
31       "tagname": "cfg",
32       "name": "associationKey",
33       "member": "Ext.data.Association",
34       "type": "String",
35       "doc": "<p>The name of the property in the data to read the association from.\nDefaults to the name of the associated model.</p>\n",
36       "private": false,
37       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
38       "linenr": 138,
39       "html_filename": "Association.html",
40       "href": "Association.html#Ext-data-Association-cfg-associationKey"
41     },
42     {
43       "tagname": "cfg",
44       "name": "foreignKey",
45       "member": "Ext.data.BelongsToAssociation",
46       "type": "String",
47       "doc": "<p>The name of the foreign key on the owner model that links it to the associated\nmodel. Defaults to the lowercased name of the associated model plus \"_id\", e.g. an association with a\nmodel called Product would set up a product_id foreign key.</p>\n\n<pre><code>Ext.define('Order', {\n    extend: 'Ext.data.Model',\n    fields: ['id', 'date'],\n    hasMany: 'Product'\n});\n\nExt.define('Product', {\n    extend: 'Ext.data.Model',\n    fields: ['id', 'name', 'order_id'], // refers to the id of the order that this product belongs to\n    belongsTo: 'Group'\n});\nvar product = new Product({\n    id: 1,\n    name: 'Product 1',\n    order_id: 22\n}, 1);\nproduct.getOrder(); // Will make a call to the server asking for order_id 22\n\n</code></pre>\n\n",
48       "private": false,
49       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/BelongsToAssociation.js",
50       "linenr": 123,
51       "html_filename": "BelongsToAssociation.html",
52       "href": "BelongsToAssociation.html#Ext-data-BelongsToAssociation-cfg-foreignKey",
53       "shortDoc": "The name of the foreign key on the owner model that links it to the associated\nmodel. Defaults to the lowercased name..."
54     },
55     {
56       "tagname": "cfg",
57       "name": "getterName",
58       "member": "Ext.data.BelongsToAssociation",
59       "type": "String",
60       "doc": "<p>The name of the getter function that will be added to the local model's prototype.\nDefaults to 'get' + the name of the foreign model, e.g. getCategory</p>\n",
61       "private": false,
62       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/BelongsToAssociation.js",
63       "linenr": 149,
64       "html_filename": "BelongsToAssociation.html",
65       "href": "BelongsToAssociation.html#Ext-data-BelongsToAssociation-cfg-getterName",
66       "shortDoc": "The name of the getter function that will be added to the local model's prototype.\nDefaults to 'get' + the name of th..."
67     },
68     {
69       "tagname": "cfg",
70       "name": "ownerModel",
71       "member": "Ext.data.Association",
72       "type": "String",
73       "doc": "<p>The string name of the model that owns the association. Required</p>\n",
74       "private": false,
75       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
76       "linenr": 120,
77       "html_filename": "Association.html",
78       "href": "Association.html#Ext-data-Association-cfg-ownerModel"
79     },
80     {
81       "tagname": "cfg",
82       "name": "primaryKey",
83       "member": "Ext.data.Association",
84       "type": "String",
85       "doc": "<p>The name of the primary key on the associated model. Defaults to 'id'.\nIn general this will be the <a href=\"#/api/Ext.data.Model-cfg-idProperty\" rel=\"Ext.data.Model-cfg-idProperty\" class=\"docClass\">Ext.data.Model.idProperty</a> of the Model.</p>\n",
86       "private": false,
87       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
88       "linenr": 128,
89       "html_filename": "Association.html",
90       "href": "Association.html#Ext-data-Association-cfg-primaryKey",
91       "shortDoc": "The name of the primary key on the associated model. Defaults to 'id'.\nIn general this will be the Ext.data.Model.idP..."
92     },
93     {
94       "tagname": "cfg",
95       "name": "reader",
96       "member": "Ext.data.Association",
97       "type": "Ext.data.reader.Reader",
98       "doc": "<p>A special reader to read associated data</p>\n",
99       "private": false,
100       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
101       "linenr": 134,
102       "html_filename": "Association.html",
103       "href": "Association.html#Ext-data-Association-cfg-reader"
104     },
105     {
106       "tagname": "cfg",
107       "name": "setterName",
108       "member": "Ext.data.BelongsToAssociation",
109       "type": "String",
110       "doc": "<p>The name of the setter function that will be added to the local model's prototype.\nDefaults to 'set' + the name of the foreign model, e.g. setCategory</p>\n",
111       "private": false,
112       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/BelongsToAssociation.js",
113       "linenr": 154,
114       "html_filename": "BelongsToAssociation.html",
115       "href": "BelongsToAssociation.html#Ext-data-BelongsToAssociation-cfg-setterName",
116       "shortDoc": "The name of the setter function that will be added to the local model's prototype.\nDefaults to 'set' + the name of th..."
117     },
118     {
119       "tagname": "cfg",
120       "name": "type",
121       "member": "Ext.data.BelongsToAssociation",
122       "type": "String",
123       "doc": "<p>The type configuration can be used when creating associations using a configuration object.\nUse 'belongsTo' to create a HasManyAssocation</p>\n\n<pre><code>associations: [{\n    type: 'belongsTo',\n    model: 'User'\n}]\n</code></pre>\n\n",
124       "private": false,
125       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/BelongsToAssociation.js",
126       "linenr": 159,
127       "html_filename": "BelongsToAssociation.html",
128       "href": "BelongsToAssociation.html#Ext-data-BelongsToAssociation-cfg-type",
129       "shortDoc": "The type configuration can be used when creating associations using a configuration object.\nUse 'belongsTo' to create..."
130     }
131   ],
132   "method": [
133     {
134       "tagname": "method",
135       "name": "BelongsToAssociation",
136       "member": "Ext.data.Association",
137       "doc": "\n",
138       "params": [
139         {
140           "type": "Object",
141           "name": "config",
142           "doc": "<p>Optional config object</p>\n",
143           "optional": false
144         }
145       ],
146       "return": {
147         "type": "void",
148         "doc": "\n"
149       },
150       "private": false,
151       "static": false,
152       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
153       "linenr": 1,
154       "html_filename": "Association.html",
155       "href": "Association.html#Ext-data-Association-method-constructor",
156       "shortDoc": "\n"
157     },
158     {
159       "tagname": "method",
160       "name": "getReader",
161       "member": "Ext.data.Association",
162       "doc": "<p>Get a specialized reader for reading associated data</p>\n",
163       "params": [
164
165       ],
166       "return": {
167         "type": "Ext.data.reader.Reader",
168         "doc": "<p>The reader, null if not supplied</p>\n"
169       },
170       "private": false,
171       "static": false,
172       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
173       "linenr": 212,
174       "html_filename": "Association.html",
175       "href": "Association.html#Ext-data-Association-method-getReader",
176       "shortDoc": "<p>Get a specialized reader for reading associated data</p>\n"
177     }
178   ],
179   "property": [
180     {
181       "tagname": "property",
182       "name": "associatedName",
183       "member": "Ext.data.Association",
184       "type": "String",
185       "doc": "<p>The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is 'Order')</p>\n",
186       "private": false,
187       "static": false,
188       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
189       "linenr": 200,
190       "html_filename": "Association.html",
191       "href": "Association.html#Ext-data-Association-property-associatedName"
192     },
193     {
194       "tagname": "property",
195       "name": "ownerName",
196       "member": "Ext.data.Association",
197       "type": "String",
198       "doc": "<p>The name of the model that 'owns' the association</p>\n",
199       "private": false,
200       "static": false,
201       "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/Association.js",
202       "linenr": 194,
203       "html_filename": "Association.html",
204       "href": "Association.html#Ext-data-Association-property-ownerName"
205     }
206   ],
207   "event": [
208
209   ],
210   "filename": "/Users/nick/Projects/sencha/SDK/platform/src/data/BelongsToAssociation.js",
211   "linenr": 1,
212   "html_filename": "BelongsToAssociation.html",
213   "href": "BelongsToAssociation.html#Ext-data-BelongsToAssociation",
214   "cssVar": [
215
216   ],
217   "cssMixin": [
218
219   ],
220   "component": false,
221   "superclasses": [
222     "Ext.data.Association"
223   ],
224   "subclasses": [
225
226   ],
227   "mixedInto": [
228
229   ],
230   "allMixins": [
231
232   ]
233 });