X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/0494b8d9b9bb03ab6c22b34dae81261e3cd7e3e6..7a654f8d43fdb43d78b63d90528bed6e86b608cc:/docs/api/Ext.data.HasManyAssociation.html diff --git a/docs/api/Ext.data.HasManyAssociation.html b/docs/api/Ext.data.HasManyAssociation.html new file mode 100644 index 00000000..6dace944 --- /dev/null +++ b/docs/api/Ext.data.HasManyAssociation.html @@ -0,0 +1,255 @@ +Ext.data.HasManyAssociation | Ext JS 4.0 Documentation +
For up to date documentation and features, visit +http://docs.sencha.com/ext-js/4-0

Sencha Documentation

+ + + + + +

Hierarchy

Ext.data.Association
Ext.data.HasManyAssociation

Represents a one-to-many relationship between two models. Usually created indirectly via a model definition:

+ + + + +
Ext.define('Product', {
+    extend: 'Ext.data.Model',
+    fields: [
+        {name: 'id',      type: 'int'},
+        {name: 'user_id', type: 'int'},
+        {name: 'name',    type: 'string'}
+    ]
+});
+
+Ext.define('User', {
+    extend: 'Ext.data.Model',
+    fields: [
+        {name: 'id',   type: 'int'},
+        {name: 'name', type: 'string'}
+    ],
+    // we can use the hasMany shortcut on the model to create a hasMany association
+    hasMany: {model: 'Product', name: 'products'}
+});
+
+ + +

+ +

Above we created Product and User models, and linked them by saying that a User hasMany Products. This gives +us a new function on every User instance, in this case the function is called 'products' because that is the name +we specified in the association configuration above.

+ + + + +

This new function returns a specialized Store which is automatically filtered to load +only Products for the given model instance:

+ + + + +
//first, we load up a User with id of 1
+var user = Ext.ModelManager.create({id: 1, name: 'Ed'}, 'User');
+
+//the user.products function was created automatically by the association and returns a Store
+//the created store is automatically scoped to the set of Products for the User with id of 1
+var products = user.products();
+
+//we still have all of the usual Store functions, for example it's easy to add a Product for this User
+products.add({
+    name: 'Another Product'
+});
+
+//saves the changes to the store - this automatically sets the new Product's user_id to 1 before saving
+products.sync();
+
+ + + + +

The new Store is only instantiated the first time you call products() to conserve memory and processing time, +though calling products() a second time returns the same store instance.

+ + + + +

Custom filtering

+ + + + +

The Store is automatically furnished with a filter - by default this filter tells the store to only return +records where the associated model's foreign key matches the owner model's primary key. For example, if a User +with ID = 100 hasMany Products, the filter loads only Products with user_id == 100.

+ + + + +

Sometimes we want to filter by another field - for example in the case of a Twitter search application we may +have models for Search and Tweet:

+ + + + +
Ext.define('Search', {
+    extend: 'Ext.data.Model',
+    fields: [
+        'id', 'query'
+    ],
+
+    hasMany: {
+        model: 'Tweet',
+        name : 'tweets',
+        filterProperty: 'query'
+    }
+});
+
+Ext.define('Tweet', {
+    extend: 'Ext.data.Model',
+    fields: [
+        'id', 'text', 'from_user'
+    ]
+});
+
+//returns a Store filtered by the filterProperty
+var store = new Search({query: 'Sencha Touch'}).tweets();
+
+ + + + +

The tweets association above is filtered by the query property by setting the filterProperty, and is +equivalent to this:

+ + + + +
var store = new Ext.data.Store({
+    model: 'Tweet',
+    filters: [
+        {
+            property: 'query',
+            value   : 'Sencha Touch'
+        }
+    ]
+});
+
+ +
Defined By

Config Options

Other Configs

 

The string name of the model that is being associated with. Required

+

The string name of the model that is being associated with. Required

+
 

The name of the property in the data to read the association from. +Defaults to the name of the associated model.

+

The name of the property in the data to read the association from. +Defaults to the name of the associated model.

+
 

True to automatically load the related store from a remote source when instantiated. +Defaults to false.

+

True to automatically load the related store from a remote source when instantiated. +Defaults to false.

+
 
Optionally overrides the default filter that is set up on the associated Store. If +this is not set, a filter is autom...

Optionally overrides the default filter that is set up on the associated Store. If +this is not set, a filter is automatically created which filters the association based on the configured +foreignKey. See intro docs for more details. Defaults to undefined

+
 
The name of the foreign key on the associated model that links it to the owner +model. Defaults to the lowercased name...

The name of the foreign key on the associated model that links it to the owner +model. Defaults to the lowercased name of the owner model plus "_id", e.g. an association with a where a +model called Group hasMany Users would create 'group_id' as the foreign key. When the remote store is loaded, +the store is automatically filtered so that only records with a matching foreign key are included in the +resulting child store. This can be overridden by specifying the filterProperty.

+ +
Ext.define('Group', {
+    extend: 'Ext.data.Model',
+    fields: ['id', 'name'],
+    hasMany: 'User'
+});
+
+Ext.define('User', {
+    extend: 'Ext.data.Model',
+    fields: ['id', 'name', 'group_id'], // refers to the id of the group that this user belongs to
+    belongsTo: 'Group'
+});
+
+ +
 
The name of the function to create on the owner model to retrieve the child store. +If not specified, the pluralized n...

The name of the function to create on the owner model to retrieve the child store. +If not specified, the pluralized name of the child model is used.

+ +
// This will create a users() method on any Group model instance
+Ext.define('Group', {
+    extend: 'Ext.data.Model',
+    fields: ['id', 'name'],
+    hasMany: 'User'
+});
+var group = new Group();
+console.log(group.users());
+
+// The method to retrieve the users will now be getUserList
+Ext.define('Group', {
+    extend: 'Ext.data.Model',
+    fields: ['id', 'name'],
+    hasMany: {model: 'User', name: 'getUserList'}
+});
+var group = new Group();
+console.log(group.getUserList());
+
+ +
 

The string name of the model that owns the association. Required

+

The string name of the model that owns the association. Required

+
 
The name of the primary key on the associated model. Defaults to 'id'. +In general this will be the Ext.data.Model.idP...

The name of the primary key on the associated model. Defaults to 'id'. +In general this will be the Ext.data.Model.idProperty of the Model.

+
 
reader : Ext.data.reader.Reader

A special reader to read associated data

+

A special reader to read associated data

+
 

Optional configuration object that will be passed to the generated Store. Defaults to +undefined.

+

Optional configuration object that will be passed to the generated Store. Defaults to +undefined.

+
 
The type configuration can be used when creating associations using a configuration object. +Use 'hasMany' to create a...

The type configuration can be used when creating associations using a configuration object. +Use 'hasMany' to create a HasManyAssocation

+ +
associations: [{
+    type: 'hasMany',
+    model: 'User'
+}]
+
+ +
Defined By

Properties

 

The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is 'Order')

+

The name of the model is on the other end of the association (e.g. if a User model hasMany Orders, this is 'Order')

+
 

The name of the model that 'owns' the association

+

The name of the model that 'owns' the association

+
Defined By

Methods

 

 

+

Parameters

  • config : Object

    Optional config object

    +

Returns

  • void    +
 

Get a specialized reader for reading associated data

+

Get a specialized reader for reading associated data

+

Returns

  • Ext.data.reader.Reader   

    The reader, null if not supplied

    +
\ No newline at end of file