Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: [SOLVED] Copy, clone or duplicate a JsonStore

  1. #1
    Ext User skaue's Avatar
    Join Date
    Sep 2008
    Location
    Troms
    Posts
    191

    Default [SOLVED] Copy, clone or duplicate a JsonStore

    I have a local JsonStore ready for use, and I use it in two comboboxes. However, I want to add an extra inital value in one of the comboboxes. At the moment I'm just adding a new entry in the JsonStore's data, but this obviously adds the data for BOTH combos, since they use the same store.

    So I need to either have some way to copy the store and use the copied store in one of the combos, OR find some other way to use same JsonStore but let one of the combos show an additional "option".

    Can JsonStore at all be copied?
    Webdeveloper from Norway

  2. #2
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    Try:
    Code:
    var records = [];
    store.each(function(r){
    	records.push(r.copy());
    });
    var store2 = new Ext.data.Store({
    	recordType: store.recordType
    });
    store2.add(records);

  3. #3
    Ext User skaue's Avatar
    Join Date
    Sep 2008
    Location
    Troms
    Posts
    191

    Default

    Thanks a lot! Now WHY didn't I just ask right away instead of struggling with this for several hours on my own.

    Here's my custom combo that takes a firstOption:
    Code:
    Facilit.App.Form.ComboBox = Ext.extend(Ext.form.ComboBox, {
       constructor: function(config)
       {
          config = config || {};
          config.listeners = config.listeners || {};
          config.firstOption = config.firstOption || null
    
          if (config.store && config.store.data && config.firstOption)
          {
             var records = [];
             config.store.each(function(r)
             {
                records.push(r.copy());
             });
             var store2 = new Ext.data.Store({
                recordType: config.store.recordType
             });
             store2.add(records);
             store2.insert(0, new config.store.recordType(config.firstOption));
             config.store = store2;
          }
    
          config = Ext.apply({
             forceSelection: true,
             typeAhead: true,
             mode: 'local',
             triggerAction: 'all',
             emptyText: Resources.GlobalStrings.DropDownListSelect,
             selectOnFocus: true
          }, config);
          Facilit.App.Form.ComboBox.superclass.constructor.call(this, config);
       }
    });
    Ext.reg('entitycombo', Facilit.App.Form.ComboBox);
    Now I can make use of it like this:
    Code:
    ...
    {
       xtype:'entitycombo',
       store: EstateStore,
       firstOption: { 
          EstateId: Resources.GuidEmpty, 
          Name:Resources.GlobalStrings.DropDownListAll, 
          NumberAndName:Resources.GlobalStrings.DropDownListAll 
       },
       hiddenName: "EstateId",
       fieldLabel: Resources.FmEntityTypeDescription.Estate.capitalize(),
       displayField: "Name"
    }
    Again, Thanks Condor!
    Webdeveloper from Norway

  4. #4

    Default

    On a performance note, you don't need to manually collect all the records like that. The getRange function of a store will, when called with no parameters, return all of the records.

    Code:
    var store2 = new Ext.data.Store({
        recordType: store.recordType
    });
    store2.add(store.getRange());

  5. #5

    Default

    Remember that the load() is asynchronous - the data won't be immediately available to you, so it is important to use an event handler that will be called when load completes. That cost me a few hours...

    Code:
    var store2 = new Ext.data.Store({
        recordType: store.recordType
    });
    
    store.on('load', function () {
        store.each(function(r){
            store2.add(r.copy());
        });
    });

  6. #6
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246

    Default

    Quote Originally Posted by rhollencamp View Post
    On a performance note, you don't need to manually collect all the records like that. The getRange function of a store will, when called with no parameters, return all of the records.

    Code:
    var store2 = new Ext.data.Store({
        recordType: store.recordType
    });
    store2.add(store.getRange());
    No that won't work. A record can only belong to ONE store, so you have to make a COPY.
    Code:
    var records = [];
    store.each(function(r){
    	records.push(r.copy());
    });
    var store2 = new Ext.data.Store({
    	recordType: store.recordType
    });
    store2.add(records);

  7. #7
    Sencha User SimoAmi's Avatar
    Join Date
    Oct 2008
    Location
    New York
    Posts
    119

    Default

    The above method by Condor works with stores already loaded or stores with static data.

    For remote/JSON stores this has been tested to work well without any dependencies:

    PHP Code:
    // the "store" variable is an instance containing the source store
    var newStore = new Ext.data.Store( {
        
    proxy store.proxy,
        
    reader store.reader,
        
    sortInfo store.sortInfo
    }); 

  8. #8
    Sencha User
    Join Date
    Jan 2011
    Location
    Rotterdam
    Posts
    25

    Default updated for extjs 4

    updated for ext 4:
    Code:
        copyStore : function (store, model){
    	var records = [];
    	var newStore = new Ext.data.Store(
    	    {
    		model : model
    	    });
    	store.each(
    	    function (r)
    	    {
    		records.push (r.copy());
    	    });
    	
    	newStore.loadRecords(records);
    	return newStore;
        },

  9. #9
    Sencha User
    Join Date
    Jun 2011
    Location
    Chennai, India
    Posts
    17

    Default Control is not going into the 'store.each' function

    I use the following code with Ext JS 3.4. The control is not going into the
    'store.each' function

    Code:
    var records = []; 
        store.each(function(r){     
            records.push(r.copy()); 
    }); 
    
    var store2 = new Ext.data.Store({     
        recordType: store.recordType 
    }); 
    store2.add(records);
    When student is ready, teacher appears

  10. #10
    Sencha User
    Join Date
    Oct 2011
    Posts
    15

    Default

    One question:

    I have one store with 1200 rows of data that I'm putting on a grid in pack of 120 rows, but i need to get ALL the data ( 1200 rows ) with filters for print it using the pluggin: print ... My idea is to use one alternative grid store, without "pagesize" parameter and get all rows .. but didnt know that is correct or not...

    thx 4 ur help

    --
    EDIT: I have read the reply in the other post But, its possible my idea ?

    --
    Yeah, it done!

    Solution: I modify "print.js" plugin with to 2 entrance param, one for the store and the other with the grid.panel. Then, I can move step by step between colums that I got with grid.panel and get the data of store with all the row with a query

    cya and thx!
    Last edited by mfruizs2; 17 Oct 2011 at 7:49 AM. Reason: DONE

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •