Thursday, 11 April 2013

How to copy records from one Store to another in Sencha Touch 2

Today, we are going to see how we can copy records from one store to another. You may came across a situation, where you need to create a new store which contains the records from the existing store and in addition to that, you need to add one or more new records.

In that Scenario, let's demonstrate this with an example, which will copy the existing store records and add one new record. Then, we will merge both the existing records and the new one using Ext.Merge.object() and finally add it to the new Store.

Define the first Store

Ext.define('MyApp.store.MyStore1', {
    extend: 'Ext.data.Store',

    config: {
        data: [
            {
                Name: 'user1',
                Age: 29,
                DateOfBirth: '10-11-1980'
            },
            {
                Name: 'user2',
                Age: 28,
                DateOfBirth: '10-1-1989'
            },
            {
                Name: 'user3',
                Age: 29,
                DateOfBirth: '10-07-1982'
            },
            {
                Name: 'user4',
                Age: 27,
                DateOfBirth: '10-02-1984'
            },
            {
                Name: 'user5',
                Age: 26,
                DateOfBirth: '4-07-1991'
            },
            {
                Name: 'user6',
                Age: 26,
                DateOfBirth: '9-10-1986'
            }
        ],
        storeId: 'MyStore1',
        fields: [
            {
                name: 'Name'
            },
            {
                name: 'Age'
            },
            {
                name: 'DateOfBirth'
            }
        ]
    }
});

Define the Second Store

Ext.define('MyApp.data.MyStore2',{
    extend: 'Ext.data.Store',
    config:{
      storeId: 'MyStore2'
    }
});

Copy Record from First Store to Second

Now, Let's get the instance of above defined stores using Ext.getStore(). Then, we are looping the records from the first store and creating a clone record using record.copy(). Using Ext.Object.merge(), we are going to merge the clone record with our new record which contains 'location' property. Finally, we are adding the resulted record to the second store using add().

var store1Obj = Ext.getStore('MyStore1');
var store2Obj = Ext.getStore('MyStore2');

store1Obj.each(function(record){

      var copiedRecord = record.copy();
      var newRecord = Ext.Object.merge(copiedRecord.data,{location:'chennai'});
      store2Obj.add(copiedRecord);
});

Now, we will loop the second store records and display the record items in console.log().  In addition to the first store records, our newly added location records are also getting displayed in the Output.

store2Obj.each(function(record){

   console.log('Name: '+record.data.Name+', Age: '+record.data.Age+', Date Of Birth: '+record.data.DateOfBirth);

});

Output:
Name: user1, Age: 29, Date Of Birth: 10-11-1980, Location: chennai
Name: user2, Age: 28, Date Of Birth: 10-1-1989, Location: chennai
Name: user3, Age: 29, Date Of Birth: 10-07-1982, Location: chennai
Name: user4, Age: 27, Date Of Birth: 10-02-1984, Location: chennai
Name: user5, Age: 26, Date Of Birth: 4-07-1991, Location: chennai
Name: user6, Age: 26, Date Of Birth: 9-10-1986, Location: chennai

Hope, you enjoyed this Post.

No comments:

Post a Comment