Clear Up
SharpKit Reference

BelongsTo Class

Namespace: Ext.data.association

Base Types

System.Object

Constructors

Name Description
BelongsTo()
BelongsTo(BelongsToConfig)
BelongsTo(object) @cfg {String} type The type configuration can be used when creating associations using a configuration object. Use 'belongsTo' to create a BelongsTo association. * associations: [{ type: 'belongsTo', model: 'User' }]

Properties

Name Description
alias
alternateClassName @author Ed Spencer @class Ext.data.association.BelongsTo @extends Ext.data.association.Association * Represents a many to one association with another model. The owner model is expected to have a foreign key which references the primary key of the associated model: * Ext.define('Category', { extend: 'Ext.data.Model', fields: [ { name: 'id', type: 'int' }, { name: 'name', type: 'string' } ] }); * Ext.define('Product', { extend: 'Ext.data.Model', fields: [ { name: 'id', type: 'int' }, { name: 'category_id', type: 'int' }, { name: 'name', type: 'string' } ], // we can use the belongsTo shortcut on the model to create a belongsTo association associations: { type: 'belongsTo', model: 'Category' } }); * In the example above we have created models for Products and Categories, and linked them together by saying that each Product belongs to a Category. This automatically links each Product to a Category based on the Product's category_id, and provides new functions on the Product model: * ## Generated getter function * The first function that is added to the owner model is a getter function: * var product = new Product({ id: 100, category_id: 20, name: 'Sneakers' }); * product.getCategory(function(category, operation) { // do something with the category object alert(category.get('id')); // alerts 20 }, this); * The getCategory function was created on the Product model when we defined the association. This uses the Category's configured {@link Ext.data.proxy.Proxy proxy} to load the Category asynchronously, calling the provided callback when it has loaded. * The new getCategory function will also accept an object containing success, failure and callback properties - callback will always be called, success will only be called if the associated model was loaded successfully and failure will only be called if the associatied model could not be loaded: * product.getCategory({ reload: true, // force a reload if the owner model is already cached callback: function(category, operation) {}, // a function that will always be called success : function(category, operation) {}, // a function that will only be called if the load succeeded failure : function(category, operation) {}, // a function that will only be called if the load did not succeed scope : this // optionally pass in a scope object to execute the callbacks in }); * In each case above the callbacks are called with two arguments - the associated model instance and the {@link Ext.data.Operation operation} object that was executed to load that instance. The Operation object is useful when the instance could not be loaded. Once the getter has been called on the model, it will be cached if the getter is called a second time. To force the model to reload, specify reload: true in the options object. * ## Generated setter function * The second generated function sets the associated model instance - if only a single argument is passed to the setter then the following two calls are identical: * // this call... product.setCategory(10); * // is equivalent to this call: product.set('category_id', 10); An instance of the owner model can also be passed as a parameter. * If we pass in a second argument, the model will be automatically saved and the second argument passed to the owner model's {@link Ext.data.Model#save save} method: * product.setCategory(10, function(product, operation) { // the product has been saved alert(product.get('category_id')); //now alerts 10 }); * //alternative syntax: product.setCategory(10, { callback: function(product, operation), // a function that will always be called success : function(product, operation), // a function that will only be called if the load succeeded failure : function(product, operation), // a function that will only be called if the load did not succeed scope : this //optionally pass in a scope object to execute the callbacks in }) * ## Customisation * Associations reflect on the models they are linking to automatically set up properties such as the {@link #primaryKey} and {@link #foreignKey}. These can alternatively be specified: * Ext.define('Product', { fields: [...], * associations: [ { type: 'belongsTo', model: 'Category', primaryKey: 'unique_id', foreignKey: 'cat_id' } ] }); * Here we replaced the default primary key (defaults to 'id') and foreign key (calculated as 'category_id') with our own settings. Usually this will not be needed.
extend @author Ed Spencer @class Ext.data.association.BelongsTo @extends Ext.data.association.Association * Represents a many to one association with another model. The owner model is expected to have a foreign key which references the primary key of the associated model: * Ext.define('Category', { extend: 'Ext.data.Model', fields: [ { name: 'id', type: 'int' }, { name: 'name', type: 'string' } ] }); * Ext.define('Product', { extend: 'Ext.data.Model', fields: [ { name: 'id', type: 'int' }, { name: 'category_id', type: 'int' }, { name: 'name', type: 'string' } ], // we can use the belongsTo shortcut on the model to create a belongsTo association associations: { type: 'belongsTo', model: 'Category' } }); * In the example above we have created models for Products and Categories, and linked them together by saying that each Product belongs to a Category. This automatically links each Product to a Category based on the Product's category_id, and provides new functions on the Product model: * ## Generated getter function * The first function that is added to the owner model is a getter function: * var product = new Product({ id: 100, category_id: 20, name: 'Sneakers' }); * product.getCategory(function(category, operation) { // do something with the category object alert(category.get('id')); // alerts 20 }, this); * The getCategory function was created on the Product model when we defined the association. This uses the Category's configured {@link Ext.data.proxy.Proxy proxy} to load the Category asynchronously, calling the provided callback when it has loaded. * The new getCategory function will also accept an object containing success, failure and callback properties - callback will always be called, success will only be called if the associated model was loaded successfully and failure will only be called if the associatied model could not be loaded: * product.getCategory({ reload: true, // force a reload if the owner model is already cached callback: function(category, operation) {}, // a function that will always be called success : function(category, operation) {}, // a function that will only be called if the load succeeded failure : function(category, operation) {}, // a function that will only be called if the load did not succeed scope : this // optionally pass in a scope object to execute the callbacks in }); * In each case above the callbacks are called with two arguments - the associated model instance and the {@link Ext.data.Operation operation} object that was executed to load that instance. The Operation object is useful when the instance could not be loaded. Once the getter has been called on the model, it will be cached if the getter is called a second time. To force the model to reload, specify reload: true in the options object. * ## Generated setter function * The second generated function sets the associated model instance - if only a single argument is passed to the setter then the following two calls are identical: * // this call... product.setCategory(10); * // is equivalent to this call: product.set('category_id', 10); An instance of the owner model can also be passed as a parameter. * If we pass in a second argument, the model will be automatically saved and the second argument passed to the owner model's {@link Ext.data.Model#save save} method: * product.setCategory(10, function(product, operation) { // the product has been saved alert(product.get('category_id')); //now alerts 10 }); * //alternative syntax: product.setCategory(10, { callback: function(product, operation), // a function that will always be called success : function(product, operation), // a function that will only be called if the load succeeded failure : function(product, operation), // a function that will only be called if the load did not succeed scope : this //optionally pass in a scope object to execute the callbacks in }) * ## Customisation * Associations reflect on the models they are linking to automatically set up properties such as the {@link #primaryKey} and {@link #foreignKey}. These can alternatively be specified: * Ext.define('Product', { fields: [...], * associations: [ { type: 'belongsTo', model: 'Category', primaryKey: 'unique_id', foreignKey: 'cat_id' } ] }); * Here we replaced the default primary key (defaults to 'id') and foreign key (calculated as 'category_id') with our own settings. Usually this will not be needed.

Methods

Name Description
createGetter() @private Returns a getter function to be placed on the owner model's prototype. We cache the loaded instance the first time it is loaded so that subsequent calls to the getter always receive the same reference. @return {Function} The getter function
createSetter() @private Returns a setter function to be placed on the owner model's prototype @return {Function} The setter function
read(Model, Reader, object) Read associated data @private @param {Ext.data.Model} record The record we're writing to @param {Ext.data.reader.Reader} reader The reader for the associated model @param {Object} associationData The raw associated data
© Copyright 2005-2011 SharpKit. All rights reserved.