Clear Up
SharpKit Reference

Model Class

Namespace: Ext.data

Base Types

System.Object

Constructors

Name Description
Model()
Model(ModelConfig)
Model(object, object, object) @cfg {String/Object/Ext.data.proxy.Proxy} proxy The {@link Ext.data.proxy.Proxy proxy} to use for this model.

Methods

Name Description
afterCommit() @private If this Model instance has been {@link #join joined} to a {@link Ext.data.Store store}, the store's afterCommit method is called
afterEdit(object) @private If this Model instance has been {@link #join joined} to a {@link Ext.data.Store store}, the store's afterEdit method is called @param {String[]} modifiedFieldNames Array of field names changed during edit.
afterReject() @private If this Model instance has been {@link #join joined} to a {@link Ext.data.Store store}, the store's afterReject method is called
beginEdit() Begins an edit. While in edit mode, no events (e.g.. the `update` event) are relayed to the containing store. When an edit has begun, it must be followed by either {@link #endEdit} or {@link #cancelEdit}.
callStore(JsString) @private Helper function used by afterEdit, afterReject and afterCommit. Calls the given method on the {@link Ext.data.Store store} that this instance has {@link #join joined}, if any. The store function will always be called with the model instance as its single argument. @param {String} fn The function to call on the store
cancelEdit() Cancels all changes made in the current edit operation.
commit(bool) Usually called by the {@link Ext.data.Store} which owns the model instance. Commits all changes made to the instance since either creation or the last commit operation. * Developers should subscribe to the {@link Ext.data.Store#update} event to have their code notified of commit operations. * @param {Boolean} silent (optional) True to skip notification of the owning store of the change. Defaults to false.
copy(JsString) Creates a copy (clone) of this Model instance. * @param {String} id (optional) A new id, defaults to the id of the instance being copied. See `{@link Ext.data.Model#id id}`. To generate a phantom instance with a new id use: * var rec = record.copy(); // clone the record Ext.data.Model.id(rec); // automatically generate a unique sequential id * @return {Ext.data.Model}
destroy(object) Destroys the model using the configured proxy. @param {Object} options Options to pass to the proxy. Config object for {@link Ext.data.Operation}. @return {Ext.data.Model} The Model instance
endEdit(bool, object) Ends an edit. If any data was modified, the containing store is notified (ie, the store's `update` event will fire). @param {Boolean} silent True to not notify the store of the change @param {String[]} modifiedFieldNames Array of field names changed during edit.
get(JsString) Returns the value of the given field @param {String} fieldName The field to fetch the value for @return {Object} The value
getAssociatedData() Gets all of the data from this Models *loaded* associations. It does this recursively - for example if we have a User which hasMany Orders, and each Order hasMany OrderItems, it will return an object like this: * { orders: [ { id: 123, status: 'shipped', orderItems: [ ... ] } ] } * @return {Object} The nested data set for the Model's loaded associations
getChanges() Gets a hash of only the fields that have been modified since this Model was created or commited. @return {Object}
getData(bool) Gets all values for each field in this model and returns an object containing the current data. @param {Boolean} includeAssociated True to also include associated data. Defaults to false. @return {Object} An object hash containing all the values in this model
getId() Returns the unique ID allocated to this model instance as defined by {@link #idProperty}. @return {Number} The id
getProxy() Returns the configured Proxy for this Model. @return {Ext.data.proxy.Proxy} The proxy
getUniqueId()
isEqual(object, object) Checks if two values are equal, taking into account certain special factors, for example dates. @private @param {Object} a The first value @param {Object} b The second value @return {Boolean} True if the values are equal
isModified(JsString) Returns true if the passed field name has been `{@link #modified}` since the load or last commit. @param {String} fieldName {@link Ext.data.Field#name} @return {Boolean}
isValid() Checks if the model is valid. See {@link #validate}. @return {Boolean} True if the model is valid.
join(Store) Tells this model instance that it has been added to a store. @param {Ext.data.Store} store The store to which this model has been added.
markDirty()
onClassExtended(object, object, object)
prepareAssociatedData(Model, object, JsString) @private This complex-looking method takes a given Model instance and returns an object containing all data from all of that Model's *loaded* associations. See {@link #getAssociatedData} @param {Ext.data.Model} record The Model instance @param {String[]} ids PRIVATE. The set of Model instance internalIds that have already been loaded @param {String} associationType (optional) The name of the type of association to limit to. @return {Object} The nested data set for the Model's loaded associations
reject(bool) Usually called by the {@link Ext.data.Store} to which this model instance has been {@link #join joined}. Rejects all changes made to the model instance since either creation, or the last commit operation. Modified fields are reverted to their original values. * Developers should subscribe to the {@link Ext.data.Store#update} event to have their code notified of reject operations. * @param {Boolean} silent (optional) True to skip notification of the owning store of the change. Defaults to false.
save(object) Saves the model instance using the configured proxy. @param {Object} options Options to pass to the proxy. Config object for {@link Ext.data.Operation}. @return {Ext.data.Model} The Model instance
set(object, object) Sets the given field to the given value, marks the instance as dirty @param {String/Object} fieldName The field to set, or an object containing key/value pairs @param {Object} value The value to set
setDirty() Marks this **Record** as `{@link #dirty}`. This method is used interally when adding `{@link #phantom}` records to a {@link Ext.data.proxy.Server#writer writer enabled store}. * Marking a record `{@link #dirty}` causes the phantom to be returned by {@link Ext.data.Store#getUpdatedRecords} where it will have a create action composed for it during {@link Ext.data.Model#save model save} operations.
setId(JsNumber) Sets the model instance's id field to the given id. @param {Number} id The new id
setProxy(object) Sets the Proxy to use for this model. Accepts any options that can be accepted by {@link Ext#createByAlias Ext.createByAlias}. * @param {String/Object/Ext.data.proxy.Proxy} proxy The proxy @return {Ext.data.proxy.Proxy}
unjoin(Store) Tells this model instance that it has been removed from the store. @param {Ext.data.Store} store The store from which this model has been removed.
validate() Validates the current data against all of its configured {@link #validations}. @return {Ext.data.Errors} The errors object

Properties

Name Description
alternateClassName @author Ed Spencer * A Model represents some object that your application manages. For example, one might define a Model for Users, Products, Cars, or any other real-world object that we want to model in the system. Models are registered via the {@link Ext.ModelManager model manager}, and are used by {@link Ext.data.Store stores}, which are in turn used by many of the data-bound components in Ext. * Models are defined as a set of fields and any arbitrary methods and properties relevant to the model. For example: * Ext.define('User', { extend: 'Ext.data.Model', fields: [ {name: 'name', type: 'string'}, {name: 'age', type: 'int'}, {name: 'phone', type: 'string'}, {name: 'alive', type: 'boolean', defaultValue: true} ], * changeName: function() { var oldName = this.get('name'), newName = oldName + " The Barbarian"; * this.set('name', newName); } }); * The fields array is turned into a {@link Ext.util.MixedCollection MixedCollection} automatically by the {@link Ext.ModelManager ModelManager}, and all other functions and properties are copied to the new Model's prototype. * Now we can create instances of our User model and call any model logic we defined: * var user = Ext.create('User', { name : 'Conan', age : 24, phone: '555-555-5555' }); * user.changeName(); user.get('name'); //returns "Conan The Barbarian" * # Validations * Models have built-in support for validations, which are executed against the validator functions in {@link Ext.data.validations} ({@link Ext.data.validations see all validation functions}). Validations are easy to add to models: * Ext.define('User', { extend: 'Ext.data.Model', fields: [ {name: 'name', type: 'string'}, {name: 'age', type: 'int'}, {name: 'phone', type: 'string'}, {name: 'gender', type: 'string'}, {name: 'username', type: 'string'}, {name: 'alive', type: 'boolean', defaultValue: true} ], * validations: [ {type: 'presence', field: 'age'}, {type: 'length', field: 'name', min: 2}, {type: 'inclusion', field: 'gender', list: ['Male', 'Female']}, {type: 'exclusion', field: 'username', list: ['Admin', 'Operator']}, {type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/} ] }); * The validations can be run by simply calling the {@link #validate} function, which returns a {@link Ext.data.Errors} object: * var instance = Ext.create('User', { name: 'Ed', gender: 'Male', username: 'edspencer' }); * var errors = instance.validate(); * # Associations * Models can have associations with other Models via {@link Ext.data.association.HasOne}, {@link Ext.data.association.BelongsTo belongsTo} and {@link Ext.data.association.HasMany hasMany} associations. For example, let's say we're writing a blog administration application which deals with Users, Posts and Comments. We can express the relationships between these models like this: * Ext.define('Post', { extend: 'Ext.data.Model', fields: ['id', 'user_id'], * belongsTo: 'User', hasMany : {model: 'Comment', name: 'comments'} }); * Ext.define('Comment', { extend: 'Ext.data.Model', fields: ['id', 'user_id', 'post_id'], * belongsTo: 'Post' }); * Ext.define('User', { extend: 'Ext.data.Model', fields: ['id'], * hasMany: [ 'Post', {model: 'Comment', name: 'comments'} ] }); * See the docs for {@link Ext.data.association.HasOne}, {@link Ext.data.association.BelongsTo} and {@link Ext.data.association.HasMany} for details on the usage and configuration of associations. Note that associations can also be specified like this: * Ext.define('User', { extend: 'Ext.data.Model', fields: ['id'], * associations: [ {type: 'hasMany', model: 'Post', name: 'posts'}, {type: 'hasMany', model: 'Comment', name: 'comments'} ] }); * # Using a Proxy * Models are great for representing types of data and relationships, but sooner or later we're going to want to load or save that data somewhere. All loading and saving of data is handled via a {@link Ext.data.proxy.Proxy Proxy}, which can be set directly on the Model: * Ext.define('User', { extend: 'Ext.data.Model', fields: ['id', 'name', 'email'], * proxy: { type: 'rest', url : '/users' } }); * Here we've set up a {@link Ext.data.proxy.Rest Rest Proxy}, which knows how to load and save data to and from a RESTful backend. Let's see how this works: * var user = Ext.create('User', {name: 'Ed Spencer', email: 'ed@sencha.com'}); * user.save(); //POST /users * Calling {@link #save} on the new Model instance tells the configured RestProxy that we wish to persist this Model's data onto our server. RestProxy figures out that this Model hasn't been saved before because it doesn't have an id, and performs the appropriate action - in this case issuing a POST request to the url we configured (/users). We configure any Proxy on any Model and always follow this API - see {@link Ext.data.proxy.Proxy} for a full list. * Loading data via the Proxy is equally easy: * //get a reference to the User model class var User = Ext.ModelManager.getModel('User'); * //Uses the configured RestProxy to make a GET request to /users/123 User.load(123, { success: function(user) { console.log(user.getId()); //logs 123 } }); * Models can also be updated and destroyed easily: * //the user Model we loaded in the last snippet: user.set('name', 'Edward Spencer'); * //tells the Proxy to save the Model. In this case it will perform a PUT request to /users/123 as this Model already has an id user.save({ success: function() { console.log('The User was updated'); } }); * //tells the Proxy to destroy the Model. Performs a DELETE request to /users/123 user.destroy({ success: function() { console.log('The User was destroyed!'); } }); * # Usage in Stores * It is very common to want to load a set of Model instances to be displayed and manipulated in the UI. We do this by creating a {@link Ext.data.Store Store}: * var store = Ext.create('Ext.data.Store', { model: 'User' }); * //uses the Proxy we set up on Model to load the Store data store.load(); * A Store is just a collection of Model instances - usually loaded from a server somewhere. Store can also maintain a set of added, updated and removed Model instances to be synchronized with the server via the Proxy. See the {@link Ext.data.Store Store docs} for more information on Stores. * @constructor Creates new Model instance. @param {Object} data An object containing keys corresponding to this model's fields, and their associated values @param {Number} id (optional) Unique ID to assign to this model instance
AssociatedData Gets all of the data from this Models *loaded* associations. It does this recursively - for example if we have a User which hasMany Orders, and each Order hasMany OrderItems, it will return an object like this: * { orders: [ { id: 123, status: 'shipped', orderItems: [ ... ] } ] } * @return {Object} The nested data set for the Model's loaded associations
defaultProxyType @cfg {String} defaultProxyType The string type of the default Model Proxy. Defaults to 'ajax'.
dirty @property {Boolean} dirty True if this Record has been modified. Read-only.
editing @property {Boolean} editing Internal flag used to track whether or not the model instance is currently being edited. Read-only.
evented @cfg {String} persistenceProperty The property on this Persistable object that its data is saved to. Defaults to 'data' (e.g. all persistable data resides in this.data.)
Id Returns the unique ID allocated to this model instance as defined by {@link #idProperty}. @return {Number} The id Sets the model instance's id field to the given id. @param {Number} id The new id
idProperty @cfg {String} idProperty The name of the field treated as this Model's unique id. Defaults to 'id'.
isModel
persistenceProperty @cfg {String} persistenceProperty The property on this Persistable object that its data is saved to. Defaults to 'data' (e.g. all persistable data resides in this.data.)
phantom @property {Boolean} phantom True when the record does not yet exist in a server-side database (see {@link #setDirty}). Any record which has a real database pk set as its id property is NOT a phantom -- it's real.
Proxy Sets the Proxy to use for this model. Accepts any options that can be accepted by {@link Ext#createByAlias Ext.createByAlias}. * @param {String/Object/Ext.data.proxy.Proxy} proxy The proxy @return {Ext.data.proxy.Proxy} Returns the configured Proxy for this Model. @return {Ext.data.proxy.Proxy} The proxy
© Copyright 2005-2011 SharpKit. All rights reserved.