articles

Home / DeveloperSection / Articles / Model in Sencha Touch

Model in Sencha Touch

Sumit Kesarwani 3511 19-Jun-2013

In this article, I’m explaining the model in sencha touch.

Models are defined as a set of fields and any arbitrary methods and properties relevant to the model. 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.

To create a model:

Drag-n-drop the model from the toolbox and give meaningful name to the model, and then add fields to the model through field configuration.

Ext.define('MyApp.model.UserModel', {

    extend: 'Ext.data.Model',
 
    config: {
        fields: [
            {
                name: 'Name'
            },
            {
                name: 'Age'
            },
            {
                name: 'Phone'
            },
            {
                name: 'Gender'
            },
            {
                name: 'Email'
            },
            {
                name: 'username'
            }
        ]
    }
}); 
Validation

Models have built-in support for validations, which are executed against the validator functions in Ext.data.validations. Validations are easy to add to models:

Ext.define('MyApp.model.UserModel', {

    extend: 'Ext.data.Model',
 
    config: {
        fields: [
            {
                name: 'Name'
            },
            {
                name: 'Age'
            },
            {
                name: 'Phone'
            },
            {
                name: 'Gender'
            },
            {
                name: 'Email'
            },
            {
                name: 'username'
            }
        ],
        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',
                matcher: /([a-z]+)[0-9]{2,3}/,
                field: 'username'
            }
        ]
    }
});

 The validations can be run by simply calling the validate function, which returns a Ext.data.Errors object:

var errors = instance.validate();

 


Updated 27-Sep-2019

Leave Comment

Comments

Liked By