1. #1
    Sencha User
    Join Date
    Oct 2010
    Location
    Edinburgh, UK
    Posts
    9
    Vote Rating
    0
    nelstrom is on a distinguished road

      0  

    Default Forcing another JSONP request from the Twitter API

    Forcing another JSONP request from the Twitter API


    This code should create a 'Timeline' tab, and populate it from Twitter search results:

    Code:
    var timeline = new Ext.Component({
    	title: 'Timeline',
    	cls: 'timeline',
    	scroll: 'vertical',
    	tpl: [
    		'<tpl for=".">',
    			'<div class="tweet">',
    					'<div class="avatar"><img src="{profile_image_url}" /></div>',
    					'<div class="tweet-content">',
    						'<h2>{from_user}</h2>',
    						'<p>{text}</p>',
    					'</div>',
    			'</div>',
    		'</tpl>'
    	]
    });
    
    var panel = new Ext.TabPanel({
    	fullscreen: true,
    	items: [timeline]
    });
    
    var refresh = function() {
    	Ext.util.JSONP.request({
    		url: 'http://search.twitter.com/search.json',
    		callbackKey: 'callback',
    		params: {
    			q: "bored",
    			rpp: 30
    		},
    		callback: function(data) {
    			var tweet_list = data.results;
    			timeline.update(tweet_list);	// Update the tweets in timeline
    		}
    	});
    };
    
    panel.getTabBar().add([
    	{xtype: 'spacer'},
    	{
    		xtype: 'button',
    		iconMask: true,
    		iconCls: 'refresh',
    		ui: 'plain',
    		style: 'margin:0;',
    		handler: refresh
    	}
    ]);
    panel.getTabBar().doLayout();
    
    refresh();
    I'm using the word 'bored' as my query, because it's mentioned on Twitter about 70 times per minute.

    I've attached the 'refresh' function to the handler of the 'refresh' button, and it is being called when I click the button, but the view never gets redrawn.

    I discovered that I could force another request to be made to Twitter by adding a meaningless query parameter, with the value randomized. E.g:

    Code:
    var refresh = function() {
    	Ext.util.JSONP.request({
    		url: 'http://search.twitter.com/search.json',
    		callbackKey: 'callback',
    		params: {
    			q: "bored",
    			rpp: 30,
    			uniquify: Math.random()
    		},
    		callback: function(data) {
    			var tweet_list = data.results;
    			timeline.update(tweet_list);	// Update the tweets in timeline
    		}
    	});
    };
    Now, when I click the refresh button, the view is updated with fresh data from Twitter.

    So it seems that if you try to submit a JSONP request with the same parameters more than once, a cached version of the data is returned. Is there a neater way of always fetching fresh data from the API?

    In this case, I'm not sure if the caching is done by Sencha Touch or by Twitter.

  2. #2
    Sencha - Community Support Team VinylFox's Avatar
    Join Date
    Mar 2007
    Location
    Baltimore, MD
    Posts
    1,501
    Vote Rating
    8
    VinylFox will become famous soon enough VinylFox will become famous soon enough

      0  

    Default


    The caching is done by the browser.

    If you used a DataView & Store with a ScriptTagProxy, then a cache busting string is appended for you.

  3. #3
    Sencha User
    Join Date
    Oct 2010
    Location
    Edinburgh, UK
    Posts
    9
    Vote Rating
    0
    nelstrom is on a distinguished road

      0  

    Default


    Thanks for confirming where the caching is being done.

    In this case, it seems like passing one extra query parameter to the JSONP request is a lot simpler than switching to the DataView+Store with a ScriptTagProxy. I'll go with the simpler solution for now.

Similar Threads

  1. JSONP.request only works once
    By micho2 in forum Sencha Touch 1.x: Discussion
    Replies: 3
    Last Post: 17 Nov 2010, 12:20 AM
  2. Interrupting jsonp request
    By lteti77 in forum Sencha Touch 1.x: Discussion
    Replies: 0
    Last Post: 7 Nov 2010, 5:26 AM
  3. JSONP Request problem
    By freshface in forum Sencha Touch 1.x: Discussion
    Replies: 8
    Last Post: 30 Aug 2010, 1:43 PM
  4. Help With JSONP Request
    By indieangler in forum Sencha Touch 1.x: Discussion
    Replies: 5
    Last Post: 7 Jul 2010, 11:03 AM
  5. Help With JSONP Request and Basics
    By indieangler in forum Sencha Touch 1.x: Discussion
    Replies: 1
    Last Post: 6 Jul 2010, 1:46 PM

Thread Participants: 1

Tags for this Thread