Thursday, 28 March 2013

How to merge objects in sencha touch 2

In sencha Touch 2,  Ext.Object.merge() is used to merge one or more objects into source object. In other Words, its used to add new properties and overwrite existing properties into source object.

This method accepts the following parameters
  • source : The first object into which to merge the others.
  • objs :  One or more objects to be merged into the first.
Let's demonstrate this by providing an example
var source = {
   id: '100',
   headline: 'This is test article',
   company:{
      name: 'opensource',
      platform: 'Extjs'
   }  
}

var object1 = {
   id: '110',
   company:{
      name: 'sencha',
      platform: 'Extjs',
      version: '4',
      mobile: 'sencha touch',
      mobileversion: '2'
   }  
}
var result = Ext.Object.merge(source,object1);
This method is very useful, if you need to merge objects into model object.

Here is the output for variable result
{
   id: '110',
   headline: 'This is test article',
   company:{
      name: 'sencha',
      platform: 'Extjs',
      version: '4',
      mobile: 'sencha touch',
      mobileversion: '2'
   }  
}

Hope, you enjoyed this Post.

No comments:

Post a Comment