Monday 11 November 2013

How to make model field 'id' unique using Uuid identifier in Sencha Touch 2

In Sencha Touch 2, by default model field 'id' will be unique. but we can't ensure that unique id's will be generated. So we can't treat this field to be primary key.  Inorder to make 'id' field behave as a unique id, you need to configure the following properties in the Model Class.

idProperty: 'id',
identifier: 'uuid',

In order for these above configuration properties to work. you need to add this 'Ext.data.identifier.Uuid' class as the requires one in the Ex.Application Class. Let's see the simple UserModel Class implementation with the user properties.

Source Code:

Ext.define('MyApp.model.UserModel', {
    extend: 'Ext.data.Model',
    alias: 'model.usermodel',

    config: {
        idProperty: 'id',
        identifier: 'uuid',

        fields: [{
            name: 'id'
        }, {
            name: 'username'
        }, {
            name: 'password'
        }, {
            name: 'email'
        }, {
            name: 'status'
        }]
    }
});

Following is the snapshot with the unique id's generated with alphanumeric characters and dashes by the model field 'id'.


Hope, you enjoyed this Post.

No comments:

Post a Comment