- Sencha Touch Tutorial
- Sencha Touch - Home
- Sencha Touch - Overview
- Sencha Touch - Environment
- Sencha Touch - Naming Convention
- Sencha Touch - Architecture
- Sencha Touch - MVC Explanation
- Sencha Touch - First App
- Sencha Touch - Build Application
- Sencha Touch - Migration Steps
- Sencha Touch - Core Concepts
- Sencha Touch - Data
- Sencha Touch - Theme
- Sencha Touch - Device Profile
- Sencha Touch - Dependencies
- Environment Detection
- Sencha Touch - Events
- Sencha Touch - Layout
- Sencha Touch - History & Support
- Sencha Touch - Upload & Download
- Sencha Touch - View Components
- Sencha Touch - Packaging
- Sencha Touch - Best Practice
- Sencha Touch Useful Resources
- Sencha Touch - Quick Guide
- Sencha Touch - Useful Resources
- Sencha Touch - Discussion
Sencha Touch - Model
Model is basically a collection of data or fields, which individually is used to store some specific type of information.
As Sencha follows the architecture based on top of base, classes can be customized to perform specific tasks.
Ext.data.Model is the base class which we need to extend while defining any model.
Defining a Model
Ext.define('Student', { extend: 'Ext.data.Model', config: { fields: [ { name: 'id', type: 'int' }, { name: 'name', type: 'string' } ] } });
Fields
Fields are to store a piece of information and collection of which is called model.
Mainly we define fields in model which are of the following types −
- Integer
- String
- Boolean
- Float
Syntax
{ name: 'id', type: 'int' }, { name: 'name', type: 'string' }, { name: 'marks', type: Float }, { name: 'newStudent', type: 'boolean' }
Validators
In Sencha Touch, model supports a number of validations to keep the data in a correct format.
Following are the validators −
Presence − Ensures these is no empty value for the name field.
Length − Limits the length of the field. It has two parameters - min and max - which defines the minimum and maximum length.
Format − Ensures the field value is as per the given expression. In the following example, it will not allow us to add any value apart from a number.
Inclusion − Ensures only the values which are defined in the list. In the following example, it only allows M and F for the value.
Exclusion − Ensures we do not allow the values which are defined in the list array. In the following example, it does not allow zero as age.
Syntax
validations: [ { type: validation type, field: on which the validation has to applied } ] validations: [ { type: 'presence', field: 'name' }, { type: 'length', field: 'name', min: 5 }, { type: 'format', field: 'age', matcher: /\d+/ }, { type: 'inclusion', field: 'gender', list: ['male', 'female'] }, { type: 'exclusion', field: 'name', list: ['admin'] } ], // now lets try to create a new user with as many validation errors as we can var newUser = Ext.create('User', { name: 'admin', age: 'twenty-nine', gender: 'not a valid gender' }); // run some validation on the new user we just created var errors = newUser.validate(); console.log('Is User valid?', errors.isValid()); // returns 'false' as there were validation errors console.log('All Errors:', errors.items); // returns the array of all errors found on this model instance console.log('Age Errors:', errors.getByField('age')); // returns the errors for the age field