1. #1
    Sencha User
    Join Date
    Jun 2007
    Posts
    35
    Vote Rating
    0
    nicosk is on a distinguished road

      0  

    Default Help changing Json Store data

    Help changing Json Store data


    I have two JsonStores.
    One is a full list of data, and the other is just the updates of the data(if any). If there is a match of the ids i should change the data of that row in one store with the data from the other.

    Code:
    updateScoreStore.each(function(upd){
    			liveScoreStore.each(function(liv){
    				if (upd.data['id'] == liv.data['id']){
    					console.log("WE HAVE A MATCH!");
    					//liv.data['currentScore'] == upd.data['currentScore'];
    				}				
    			});
    		});
    Is this a correct way to compare the 2 stores. Also, how do i change the values of liveScoreStore with the data of updateScoreStore

  2. #2
    Sencha User
    Join Date
    Nov 2009
    Posts
    61
    Vote Rating
    0
    tomtom- is on a distinguished road

      0  

    Default


    A solution could be :
    Code:
    updateScoreStore.each(function(upd){
        if(liveScoreStore.find('id', upd.get('id'), 0, false, true) == (-1))
           liveScoreStore.loadData(upd.data, true); 
    });

  3. #3
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    If you also configured the id as the idProperty of the store reader then using store.indexOfId is much faster than a tablescan.

  4. #4
    Sencha User
    Join Date
    Jun 2007
    Posts
    35
    Vote Rating
    0
    nicosk is on a distinguished road

      0  

    Default


    Condor, do you mean something like this?
    Code:
    var XMLReader = new Ext.data.XmlReader ({
    			record: 'Match',
    			idProperty: 'Match@id'
    		},[
    			{name: 'mid', 			mapping: '@id'},
    			{name: 'startTime', 	mapping: '@startTime'},
    			{name: 'statusType', 	mapping: '@statustype'},
    			{name: 'status', 		mapping: '@status'},
    			{name: 'home', 			mapping: 'Home@name'},
    			{name: 'homeStanding', 	mapping: 'Home@standing'},
    			{name: 'away', 			mapping: 'Away@name'},
    			{name: 'awayStanding', 	mapping: 'Away@standing'},
    			{name: 'country', 		mapping: 'Information > country'},
    			{name: 'league', 		mapping: 'Information > league'},
    			{name: 'currentScore', 	mapping: 'Results > Result@value'}
    		]);
    And then
    Code:
    updateScoreStore.each(function(upd){
        if(liveScoreStore.indexOf(upd.get['mid']))
           //change liveScoreStore row data
    });
    Thanks
    Nicos

  5. #5
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Almost.
    1. The idProperty should be '@id'.
    2. The loop should contain liveScoreStore.indexOf(upd.id)

  6. #6
    Sencha User
    Join Date
    Jun 2007
    Posts
    35
    Vote Rating
    0
    nicosk is on a distinguished road

      0  

    Default


    Thank you Condor! Works great.
    Since we are on the subject, how to change the liveScoreStore with the date from the updateStore, so the changes will be visible on the grid?
    They share the same reader, and the data is exactly the same.

    Code:
        liveScoreStore = new Ext.data.GroupingStore({
     		url: "update/soccerLive.xml",
    		autoDestroy : true,
    		reader: XMLReader, 
    	    sortInfo:{field: 'mid', direction: "ASC"},
            groupField:'status'
    	});
    
    	updateScoreStore = new Ext.data.GroupingStore({
    		url: "update/soccerUpdate.xml",
    		reader: XMLReader, 
    	    sortInfo:{field: 'mid', direction: "ASC"},
            groupField:'status'		
    	});
    This fails to work
    Code:
    	function processUpdate(){
    		console.log("In processUpdate. " + updateScoreStore.getCount());
    		var grid = Ext.getCmp('liveScores');
    		updateScoreStore.each(function(upd){
    			if (liveScoreStore.indexOf(upd.id)) {
    				console.log("Match: " + upd.id + "(" + upd.get("home") + " Vs " + upd.get("away") + ")");
    				var myRecord = grid.getStore().getById(upd.id);
    				myRecord.set('currentScore', upd.get('currentScore'));
    				grid.getStore().commitChanges();
    			} else {
    				console.log("Adding row...")
    				//liveScoreStore.add(upd.record);
    			}
    			
    		});
    	}
    How can i do it correctly?

    Regards,
    Nicos

  7. #7
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Code:
    function processUpdate(){
        var liveScoreStore = Ext.getCmp('liveScores').getStore();
        updateScoreStore.each(function(rec){
            var rec2 = liveScoreStore.getById(rec.id);
            if (rec2) {
                rec2.set('currentScore', rec.get('currentScore'));
                rec2.commit();
            } else {
                liveScoreStore.add(rec.copy());
            }
        });
    }

  8. #8
    Sencha User
    Join Date
    Jun 2007
    Posts
    35
    Vote Rating
    0
    nicosk is on a distinguished road

      0  

    Default


    Excellent. Thank you Condor!

Similar Threads

  1. JSON Store isn't loading JSON Data
    By WhirlingDerBing in forum Ext 3.x: Help & Discussion
    Replies: 3
    Last Post: 3 Nov 2009, 4:37 PM
  2. Second json store is created under each returned json Store.data.item
    By daddyschmack in forum Ext 2.x: Help & Discussion
    Replies: 1
    Last Post: 19 Aug 2009, 4:34 AM
  3. Grid doesn't refresh it's content, after changing data in it's store
    By Surgeon in forum Ext 2.x: Help & Discussion
    Replies: 8
    Last Post: 4 Dec 2008, 11:44 AM
  4. Dynamically Changing url of Data Store
    By fangzhouxing in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 17 Jul 2008, 10:49 PM
  5. Dynamic changing JSON store root
    By sistasi in forum Ext 2.x: Help & Discussion
    Replies: 1
    Last Post: 9 Apr 2008, 2:49 AM

Thread Participants: 2