|
| Model () |
|
virtual string | GetIDProperty () |
|
string | ToScript (Control owner) |
|
virtual void | Register (bool client) |
|
virtual void | RegisterOnClient () |
|
virtual void | Unregister (bool client) |
|
virtual void | UnregisterOnClient () |
|
Model.Builder | ToBuilder () |
|
override IControlBuilder | ToNativeBuilder () |
|
| Model (Config config) |
|
virtual void | AddListener (string eventName, JFunction fn) |
| Appends an event handler to this component More...
|
|
virtual void | AddListener (string eventName, JFunction fn, string scope) |
| Appends an event handler to this component More...
|
|
virtual void | AddListener (string eventName, JFunction fn, string scope, HandlerConfig options) |
| Appends an event handler to this component More...
|
|
virtual void | AddListener (string eventName, string fn) |
| Appends an event handler to this component More...
|
|
virtual void | AddListener (string eventName, string fn, string scope) |
| Appends an event handler to this component More...
|
|
virtual void | AddListener (string eventName, string fn, string scope, HandlerConfig options) |
| Appends an event handler to this component More...
|
|
virtual void | AddManagedListener (string item, string eventName, string fn, string scope, HandlerConfig options) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | AddManagedListener (string item, string eventName, string fn, string scope) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | AddManagedListener (string item, string eventName, string fn) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | ClearListeners () |
| Removes all listeners for this object including the managed listeners More...
|
|
virtual void | ClearManagedListeners () |
| Removes all managed listeners for this object. More...
|
|
virtual void | EnableBubble (params string[] events) |
| Enables events fired by this Observable to bubble up an owner hierarchy by calling this.getBubbleTarget() if present. There is no implementation in the Observable base class. This is commonly used by Ext.Components to bubble events to owner Containers. See Ext.AbstractComponent-getBubbleTarget. The default implementation in Ext.AbstractComponent returns the AbstractComponent's immediate owner. But if a known target is required, this can be overridden to access the required target more quickly. More...
|
|
virtual void | FireEvent (string eventName, params object[] args) |
| Fires the specified event with the passed parameters (minus the event name) More...
|
|
virtual void | FireEventedAction (string eventName, object[] args, string fn) |
| Fires the specified event with the passed parameters and executes a function (action). Evented Actions will automatically dispatch a 'before' event passing. This event will be given a special controller that allows for pausing/resuming of the event flow. By pausing the controller the updater and events will not run until resumed. Pausing, however, will not stop the processing of any other before events. More...
|
|
virtual void | FireEventedAction (string eventName, object[] args, string fn, string scope) |
| Fires the specified event with the passed parameters and executes a function (action). Evented Actions will automatically dispatch a 'before' event passing. This event will be given a special controller that allows for pausing/resuming of the event flow. By pausing the controller the updater and events will not run until resumed. Pausing, however, will not stop the processing of any other before events. More...
|
|
virtual void | FireEventedAction (string eventName, object[] args, string fn, string scope, object[] fnArgs) |
| Fires the specified event with the passed parameters and executes a function (action). Evented Actions will automatically dispatch a 'before' event passing. This event will be given a special controller that allows for pausing/resuming of the event flow. By pausing the controller the updater and events will not run until resumed. Pausing, however, will not stop the processing of any other before events. More...
|
|
virtual void | FireEventedAction (string eventName, object[] args, JFunction fn) |
| Fires the specified event with the passed parameters and executes a function (action). Evented Actions will automatically dispatch a 'before' event passing. This event will be given a special controller that allows for pausing/resuming of the event flow. By pausing the controller the updater and events will not run until resumed. Pausing, however, will not stop the processing of any other before events. More...
|
|
virtual void | FireEventedAction (string eventName, object[] args, JFunction fn, string scope) |
| Fires the specified event with the passed parameters and executes a function (action). Evented Actions will automatically dispatch a 'before' event passing. This event will be given a special controller that allows for pausing/resuming of the event flow. By pausing the controller the updater and events will not run until resumed. Pausing, however, will not stop the processing of any other before events. More...
|
|
virtual void | FireEventedAction (string eventName, object[] args, JFunction fn, string scope, object[] fnArgs) |
| Fires the specified event with the passed parameters and executes a function (action). Evented Actions will automatically dispatch a 'before' event passing. This event will be given a special controller that allows for pausing/resuming of the event flow. By pausing the controller the updater and events will not run until resumed. Pausing, however, will not stop the processing of any other before events. More...
|
|
virtual void | On (string eventName, string fn) |
| Appends an event handler to this element (shorthand for addListener) More...
|
|
virtual void | On (string eventName, string fn, string scope) |
| Appends an event handler to this element (shorthand for addListener) More...
|
|
virtual void | On (string eventName, string fn, string scope, HandlerConfig options) |
| Appends an event handler to this element (shorthand for addListener) More...
|
|
virtual void | On (string eventName, JFunction fn) |
| Appends an event handler to this element (shorthand for addListener) More...
|
|
virtual void | On (string eventName, JFunction fn, string scope) |
| Appends an event handler to this element (shorthand for addListener) More...
|
|
virtual void | On (string eventName, JFunction fn, string scope, HandlerConfig options) |
| Appends an event handler to this element (shorthand for addListener) More...
|
|
virtual void | RelayEvents (string origin, string[] events) |
| Relays selected events from the specified Observable as if the events were fired by this. More...
|
|
virtual void | RemoveListener (string eventName, string fn) |
| Removes an event handler. More...
|
|
virtual void | RemoveListener (string eventName, string fn, string scope) |
| Removes an event handler. More...
|
|
virtual void | RemoveManagedListener (string item, string eventName, string fn) |
| Removes listeners that were added by the mon method. More...
|
|
virtual void | RemoveManagedListener (string item, string eventName, string fn, string scope) |
| Removes listeners that were added by the mon method. More...
|
|
virtual void | ResumeEvents () |
| Resume firing events. (see suspendEvents) If events were suspended using the queueSuspended parameter, then all events fired during event suspension will be sent to any listeners now. More...
|
|
virtual void | SuspendEvents (bool queueSuspended) |
| Suspend the firing of all events. (see resumeEvents) More...
|
|
virtual void | SuspendEvents () |
| Suspend the firing of all events. (see resumeEvents) More...
|
|
virtual void | SuspendEvent (string name) |
| Suspends firing of the named event(s). More...
|
|
virtual void | ResumeEvent (string name) |
| Resumes firing of the named event(s). More...
|
|
virtual void | RemoveDirectListener (string name) |
|
virtual void | Un (string eventName, string fn) |
| Removes a listener (shorthand for removeListener) More...
|
|
virtual void | Un (string eventName, string fn, string scope) |
| Removes a listener (shorthand for removeListener) More...
|
|
virtual void | Mon (Element el, string eventName, JFunction fn) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | Mon (Observable el, string eventName, JFunction fn) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | Mon (Element el, string eventName, JFunction fn, string scope) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | Mon (Observable el, string eventName, JFunction fn, string scope) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | Mon (Element el, string eventName, string fn, string scope, HandlerConfig options) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | Mon (Observable el, string eventName, string fn, string scope, HandlerConfig options) |
| Adds listeners to any Observable object (or Element) which are automatically removed when this AbstractComponent is destroyed. More...
|
|
virtual void | Mun (Element el, string eventName, string fn) |
| Removes listeners that were added by the mon method. More...
|
|
virtual void | Mun (Observable el, string eventName, string fn) |
| Removes listeners that were added by the mon method. More...
|
|
virtual void | Mun (Element el, string eventName, string fn, string scope) |
| Removes listeners that were added by the mon method. More...
|
|
virtual void | Mun (Observable el, string eventName, string fn, string scope) |
| Removes listeners that were added by the mon method. More...
|
|
delegate void | OnBeforeClientInitializedHandler (Observable sender) |
|
delegate void | OnAfterClientInitializedHandler (Observable sender) |
|
virtual bool | HasExplicitValue (string name) |
|
virtual T | ToBuilder< T > () |
|
T | Apply< T > (IApply config) |
|
object | Apply (IApply config) |
|
BaseControl | Apply (object from, bool ignoreDefaultValues) |
|
BaseControl | Apply (object from) |
|
virtual bool | HasLayout () |
|
virtual bool | HasContent () |
|
virtual string | ResolveUrlLink (string url) |
|
bool | IsInHierarchy (params Type[] types) |
|
T | GetParentByLevel< T > (int level) |
|
virtual string | DefineInstance () |
|
virtual string | DefineInstance (string config) |
|
virtual string | DefineInstance (string instanceOf, string config) |
|
virtual string | ToConfig () |
|
virtual string | ToConfig (LazyMode mode) |
|
virtual string | ToConfig (LazyMode mode, bool registerResourcesInManager) |
|
virtual string | ToScript (bool selfRendering) |
|
virtual string | ToScript () |
|
string | ToScript (RenderMode mode, string element) |
|
string | ToScript (RenderMode mode, string element, bool selfRendering) |
|
string | ToScript (RenderMode mode, string element, int index) |
|
string | ToScript (RenderMode mode, string element, int index, bool selfRendering) |
|
string | ToScript (RenderMode mode, string element, int? index, bool? selfRendering, bool forceResources, string method, bool forceLazy, bool clearContainer) |
|
virtual void | AddScript (string script) |
| Adds the script to be be called on the client. More...
|
|
virtual void | AddScript (string template, params object[] args) |
| Adds the script to be be called on the client. The script is formatted using the template and args. More...
|
|
virtual void | Set (string name, object value) |
|
virtual void | Set (ScriptPosition position, string name, object value) |
|
virtual void | Call (string name) |
|
virtual void | Call (string name, params object[] args) |
|
virtual void | Call (string name, string[] arg) |
| Calls the JavaScript function on the Control with a string[] parameter More...
|
|
virtual void | Call (ScriptPosition mode, string name, params object[] args) |
|
virtual string | SelfRender () |
|
virtual void | AddBeforeClientInitScript (string script) |
| Adds the script directly before the ClientInitScript. More...
|
|
virtual void | AddAfterClientInitScript (string script) |
| Adds the script directly after the ClientInitScript. More...
|
|
override void | RenderBeginTag (HtmlTextWriter writer) |
|
virtual void | Render (bool selfRendering) |
|
virtual void | Render (Control control) |
|
virtual void | Render () |
|
virtual void | Render (string element, RenderMode mode) |
|
virtual void | Render (string element, int index, RenderMode mode) |
|
virtual void | Render (Control control, RenderMode mode) |
|
virtual void | Render (Control control, int index, RenderMode mode) |
|
virtual void | Render (string element, RenderMode mode, bool selfRendering) |
|
virtual void | Render (string element, int index, RenderMode mode, bool selfRendering) |
|
virtual void | Render (RenderMode mode, string element, int? index, bool? selfRendering, bool forceResources, string method, bool forceLazy, bool clearContainer) |
|
virtual void | Render (Control control, RenderMode mode, bool selfRendering) |
|
virtual void | Render (Control control, int index, RenderMode mode, bool selfRendering) |
|
virtual void | AddTo (string element) |
|
virtual void | AddTo (Control control) |
|
virtual void | AddTo (string element, bool selfRendering) |
|
virtual void | AddTo (Control control, bool selfRendering) |
|
virtual void | InsertTo (int index, string element) |
|
virtual void | InsertTo (int index, Control control) |
|
virtual void | InsertTo (int index, string element, bool selfRendering) |
|
virtual void | InsertTo (int index, Control control, bool selfRendering) |
|
virtual void | Update () |
|
virtual bool | SuspendScripting () |
|
virtual void | ResumeScripting (bool oldValue) |
|
virtual void | ResumeScripting () |
|
virtual void | Destroy () |
|
List< T > | GetCustomResources< T > () |
|
virtual void | SetResources () |
|
List< ClientStyleItem > | GetStyles () |
|
List< ClientStyleItem > | GetThemes () |
|
List< ClientScriptItem > | GetScripts () |
|
string | GetGeneratedScripts () |
| Get generated and added javascript methods calling More...
|
|
virtual bool | HasOwnState (string name) |
|
virtual void | SetControlFor (ModelMetadata meta) |
|
|
override string | InstanceOf [get] |
|
override string | ConfigIDProxy [get] |
|
static bool? | SuppressChecking [get, set] |
|
virtual bool | ConvertOnSet [get, set] |
| Set to false to prevent any converters from being called during a set operation. Defaults to true. More...
|
|
virtual string | Name [get, set] |
| The model name. Required More...
|
|
virtual string | BelongsTo [get, set] |
| One or more BelongsTo associationa for this model. More...
|
|
string | BelongsToProxy [get] |
|
virtual string | HasMany [get, set] |
| One or more HasMany associations for this model. More...
|
|
string | HasManyProxy [get] |
|
virtual string | HasOne [get, set] |
| One or more HasOne associations for this model. More...
|
|
string | HasOneProxy [get] |
|
virtual string | ClientIdProperty [get, set] |
| The name of a property that is used for submitting this Model's unique client-side identifier to the server when multiple phantom records are saved as part of the same Operation. In such a case, the server response should include the client id for each record so that the server response data can be used to update the client-side records if necessary. This property cannot have the same name as any of this Model's fields. More...
|
|
virtual string | ChildType [get, set] |
| This config is only actual for models to be used in a TreeStore. The class name of child nodes to create when reading child nodes from raw data. By default the type configured into the TreeStore is used. This is one way of creating heterogeneous nodes in a tree. To do this through data types passed from the server, use the Ext.data.reader.Reader.typeProperty. For example in the case of a hidden root node, you'd use the default type at level zero. See TreePanel's documentation for an example. Important If you are using this declaration on your tree models, and have a hidden root node, you MUST create a special root model definition which declares the type of its children. If you allow the TreeStore to create a root node of the same type as the first level of visible nodes then the reader will atempt to read the wrong type of child node for the root. More...
|
|
virtual string | Extend [get, set] |
|
virtual ModelFieldCollection | Fields [get, set] |
| The fields for this model. This is an Array of Field definition objects. A Field definition may simply be the name of the Field, but a Field encapsulates data type, custom conversion of raw data, and a mapping property to specify by name of index, how to extract a field's value from a raw data object, so it is best practice to specify a full set of Field config objects. More...
|
|
virtual string | IDProperty [get, set] |
| The name of the field treated as this Model's unique id (defaults to 'id'). More...
|
|
virtual ProxyCollection | Proxy [get] |
| The Proxy object which provides access to a data object. More...
|
|
virtual ProxyCollection | DefaultProxy [get] |
| The default proxy config to use for instances of this Model when no proxy is configured on the instance. When specified, the model will use this proxy instead of requesting one from the Session. More...
|
|
virtual string | DefaultProxyType [get, set] |
| The default proxy type to use for instances of this Model when no proxy is configured on the instance. When specified, the model will use this proxy instead of requesting one from the Session. More...
|
|
virtual AssociationCollection?? | Associations [get] |
| Models associations More...
|
|
virtual object | Summary [get, set] |
| Summary fields are a special kind of field that is used to assist in creating an aggregation for this model. More...
|
|
virtual ValidatorCollection | Validators [get] |
| An array of validators for this model. More...
|
|
virtual ModelIdGeneratorCollection?? | Identifier [get] |
| The id generator to use for this model. The default id generator does not generate values for the idProperty. More...
|
|
virtual string | ValidationSeparator [get, set] |
| If specified this property is used to concatenate multiple errors for each field as reported by the validators. Defaults to: null More...
|
|
virtual string | VersionProperty [get, set] |
| If specified, this is the name of the property that contains the entity "version". The version property is used to manage a long-running transaction and allows the detection of simultaneous modification. The way a version property is used is that the client receives the version as it would any other entity property. When saving an entity, this property is always included in the request and the server uses the value in a "conditional update". If the current version of the entity on the server matches the version property sent by the client, the update is allowed. Otherwise, the update fails. On successful update, both the client and server increment the version. This is done on the server in the conditional update and on the client when it receives a success on its update request. More...
|
|
override ConfigOptionsCollection | ConfigOptions [get] |
|
virtual List< Observable >?? | LazyItems [get] |
|
virtual bool | SingleItemMode [get] |
|
virtual ConfigItemCollection?? | CustomConfig [get] |
| Collection of custom js config More...
|
|
DynamicConfigDictionary | ConfigsProxy [get] |
|
virtual string | DirectEventsKey [get] |
|
MessageBusListeners | MessageBusListeners [get] |
|
MessageBusDirectEvents | MessageBusDirectEvents [get] |
|
virtual string | MessageBusListenersProxy [get] |
|
virtual string | MessageBusDirectEventsProxy [get] |
|
override ConfigOptionsCollection | ConfigOptions [get] |
|
string | ProductName [get] |
| The product name More...
|
|
virtual string | Version [get] |
| The Version number of this build More...
|
|
string | DynamicID [get] |
|
override string | ID [get, set] |
| The unique id of this component instance (defaults to an auto-assigned id). Components created with an id may be accessed globally using Ext.getCmp. You can use the itemId config, and ComponentQuery which provides selector-based searching for Sencha Components analogous to DOM querying. The Container class contains shortcut methods to query its descendant Components by selector. Note that this id will also be used as the element id for the containing HTML element that is rendered to the page for this component. This allows you to write id-based CSS rules to style the specific instance of this component uniquely, and also to select sub-elements using this component's id as the parent. Note: to avoid complications imposed by a unique id also see itemId. Note: to access the container of a AbstractComponent see ownerCt. More...
|
|
override string | ClientID [get] |
|
virtual string | ItemID [get, set] |
| An itemId can be used as an alternative way to get a reference to a component when no object reference is available. Instead of using an id with Ext.getCmp, use itemId with Ext.container.Container.getComponent which will retrieve itemId's or id's. Since itemId's are an index to the container's internal MixedCollection, the itemId is scoped locally to the container – avoiding potential conflicts with Ext.ComponentMgr which requires a unique id More...
|
|
virtual string???? | ConfigID [get] |
|
virtual string | BaseClientID [get] |
| The base .ClientID property derived from .NET Framework More...
|
|
virtual string | ConfigIDProxy [get] |
|
bool | HasOwnIDMode [get] |
|
virtual IDMode | IDMode [get, set] |
| Options for controlling how the .ClientID property is rendered in the client. More...
|
|
virtual bool | StopIDModeInheritance [get, set] |
|
virtual bool | StopLazyModeInheritance [get, set] |
|
virtual LazyMode? | LazyMode [get, set] |
| Options for controlling how the lazy control is instantiated in the client. More...
|
|
bool | HasOwnNamespace [get] |
|
virtual string | ClientNamespace [get] |
|
virtual string | Namespace [get, set] |
|
virtual string? | ClientNamespaceProxy [get] |
|
virtual string??? | InitialConfig [get] |
| This AbstractComponent's initial configuration specification. Read-only. More...
|
|
virtual bool | AutoDataBind [get, set] |
|
virtual FactoryConfig | Factory [get, set] |
|
virtual string | FactoryAlias [get, set] |
|
virtual string | FactoryInstance [get, set] |
|
virtual bool | TemplateWidget [get, set] |
|
virtual string | TemplateWidgetFnName [get, set] |
|
virtual ConfigOptionsCollection | ConfigOptions [get] |
|
virtual ConfigOptionsExtraction | ConfigOptionsExtraction [get] |
|
virtual DefaultValueMode | DefaultValueMode [get, set] |
|
virtual bool | RemoveContainer [get] |
|
virtual bool | IsMVC [get] |
|
static ClientScriptItem | UXScriptItem [get] |
|
static ClientStyleItem | UXStyleItem [get] |
|
virtual bool | IsDebugging [get] |
|
virtual bool | IsAjaxRequestInitiator [get] |
|
virtual bool | IsDefault [get] |
| Does this object currently represent it's default state. More...
|
|
virtual bool | IsLazy [get] |
|
virtual string? | ContainerID [get] |
|
virtual string | ContainerStyle [get] |
|
virtual bool | IsInHead [get] |
|
virtual bool | IsInForm [get] |
|
virtual string | ClientForm [get] |
|
virtual System.Web.UI.HtmlControls.HtmlForm | ParentForm [get] |
|
virtual AbstractComponent | ParentComponent [get] |
|
virtual BaseControl | ParentWebControl [get] |
|
virtual string | InstanceOf [get] |
|
virtual string | AliasPrefix [get] |
|
virtual string? | ClassName [get] |
|
virtual string | CallID [get] |
|
virtual bool | IsGeneratedID [get] |
|
bool | IsDynamic [get, set] |
| Set to true if a control is meant to be dynamically rendered. In absolutely most cases, this is set to true automatically if needed and originally the property was internal. Though, in some very specific and edge cases a developer might need to set it to true by his own. Setting it to true you should be absolutelly sure it is required. For example, if it is recommended by Ext.NET support team. Also it can be changed only to true, but not backwards. Defaults to false. More...
|
|
virtual bool | ContentUpdated [get, set] |
|
string | BeforeScript [get] |
|
string | AfterScript [get] |
|
virtual AbstractComponent | ParentComponentNotLazyOrDynamic [get] |
|
virtual bool | RenderTags [get] |
|
bool | RegisterAllResources [get, set] |
|
override string | ToolTip [get, set] |
|
override string | AccessKey [get, set] |
|
override Color | BackColor [get, set] |
|
override Color | BorderColor [get, set] |
|
override System.Web.UI.WebControls.BorderStyle | BorderStyle [get, set] |
|
override System.Web.UI.WebControls.Unit | BorderWidth [get, set] |
|
override string | CssClass [get, set] |
|
override System.Web.UI.WebControls.FontInfo | Font [get] |
|
override Color | ForeColor [get, set] |
|
override Unit | Height [get, set] |
|
override Unit | Width [get, set] |
|
virtual string | PostBackArgument [get, set] |
|
virtual string | PostBackConfig [get] |
|
virtual bool | HasResourceManager [get] |
|
virtual ResourceManager | ResourceManager [get] |
|
virtual List< ResourceItem > | Resources [get] |
|
virtual List< ClientResourceItem > | ResourceItems [get] |
|
virtual List< ResourceItem > | GlobalResources [get] |
|
virtual string? | ClientInitScript [get] |
|
virtual bool | IsInUpdatePanelRefresh [get] |
|
virtual Control | MyUpdatePanel [get] |
|
System.Web.Mvc.HtmlHelper?? | HtmlHelper [get, set] |
|
System.Web.Mvc.ViewContext | ViewContext [get, set] |
|
System.Web.Mvc.ControllerBase | Controller [get, set] |
|
virtual ViewDataDictionary | ViewData [get] |
|
virtual ControllerContext | ControllerContext [get] |
|
virtual string | ControlFor [get, set] |
|
virtual bool | IDFromControlFor [get, set] |
|
string | FormatControlForValue [get, set] |
|
Func< object, object > | ConvertControlForValue [get, set] |
|
ConfigOptionsCollection | ConfigOptions [get] |
|
ConfigOptionsExtraction | ConfigOptionsExtraction [get] |
|
DefaultValueMode | DefaultValueMode [get, set] |
|
List< Observable > | LazyItems [get] |
|
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 model manager, and are used by 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.regModel('User', { 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 MixedCollection automatically by the 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.ModelManager.create({ name : 'Conan', age : 24, phone: '555-555-5555' }, 'User');
user.changeName(); user.get('name'); //returns "Conan The Barbarian" Validators
Models have built-in support for validations, which are executed against the validator functions in Ext.data.validations (see all validation functions). Validators are easy to add to models:
Ext.regModel('User', { 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 validate function, which returns a Ext.data.Errors object:
var instance = Ext.ModelManager.create({ name: 'Ed', gender: 'Male', username: 'edspencer' }, 'User');
var errors = instance.validate(); Associations
Models can have associations with other Models via belongsTo and 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.regModel('Post', { fields: ['id', 'user_id'],
belongsTo: 'User', hasMany : {model: 'Comment', name: 'comments'} });
Ext.regModel('Comment', { fields: ['id', 'user_id', 'post_id'],
belongsTo: 'Post' });
Ext.regModel('User', { fields: ['id'],
hasMany: [ 'Post', {model: 'Comment', name: 'comments'} ] }); See the docs for Ext.data.BelongsToAssociation and Ext.data.HasManyAssociation for details on the usage and configuration of associations. Note that associations can also be specified like this:
Ext.regModel('User', { 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 Proxy, which can be set directly on the Model:
Ext.regModel('User', { fields: ['id', 'name', 'email'],
proxy: { type: 'rest', url : '/users' } }); Here we've set up a 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.ModelManager.create({name: 'Ed Spencer', email: 'ed@se.nosp@m.ncha.nosp@m..com'}, 'User');
user.save(); //POST /users Calling 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 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 Store:
var store = new 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 Store docs for more information on Stores.