PDA

View Full Version : my refresh twitter function is not working



rolfiee
22 Aug 2011, 4:29 AM
Hi there,

This is the first application that i try. I have made a refresh function in it. But it won't work.
It seems that the function gridRefresh does not update the resultPanel with new data.



Ext.setup({
onReady: function() {
var topToolbar = new Ext.Toolbar({
dock : 'top',
ui: 'dark',
title: 'Sencha Twitter Search'
});


var tpl = new Ext.XTemplate(
'<div id="tweet_container">',
'<tpl for=".">',
'<div class="tweet_data">',
'<div class="tweet_avatar">',
'<img width="30" height="30" src="{profile_image_url}"/>',
'</div>',
'<div class="tweet_content">',
'<a class="user" href="http://twitter.com/{from_user}">{from_user}</a>&nbsp;',
'{text}',
'</div>',
'<div class="clear"></div>',
'</div>',
'</tpl>',
'</div>'
);


var resultPanel = new Ext.Panel({
layout: 'fit',
style: 'padding-bottom: 10px;',
tpl: tpl
});


var searchPanel = new Ext.Panel({
padding: 10,
layout: {
type: 'hbox',
align: 'stretch'
},
items: [{
flex: 4,
xtype: 'textfield',
style: 'margin-right: 10px;',
id: 'textquery'
},{
flex: 2,
xtype: 'button',
text: 'Search',
handler: function() {
var query = Ext.getCmp("textquery").getValue();
Ext.Ajax.request({
url: 'search.php?act=search&q='+query,
success: function(e) {
var obj = Ext.util.JSON.decode(e.responseText);
var msg = obj.results;
var html = tpl.apply(msg);
resultPanel.update(html);
}
});
}
}]
});



var t = setInterval('gridRefresh()',10000);
function gridRefresh(){
Ext.Ajax.request({
url: 'search.php?act=search&q=earth',
success: function(e) {
var obj = Ext.util.JSON.decode(e.responseText);
var msg = obj.results;
var html = tpl.apply(msg);
resultPanel.update(html);
}
});
}


var myPanel = new Ext.Panel({
dockedItems: [topToolbar],
items: [searchPanel, resultPanel],
scroll: 'vertical',
style: 'background: #DDEEF6;',
fullscreen : true
});


}


});


Can anybody help me out.

Thanks!

jratcliff
22 Aug 2011, 5:13 AM
have you put in any breakpoints to confirm that the success callback is called? If it is being called, what does the data look like?

rolfiee
22 Aug 2011, 5:31 AM
Thanks for you reply. Here is the search.php code:


<?phpif (isset($_GET["act"]) && $_GET["act"] == "search") {
$url = 'http://search.twitter.com/search.json?q='.$_GET["q"];


$content = file_get_contents($url);
$array = json_decode($content);


$data = array();


foreach ($array->results as $var => $value)
{
// adding url
$pattern = '/\b(https?:\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i';
preg_match_all($pattern, $value->text, $regs);
$loop = count($regs[0]);


for ($i = 0; $i < $loop; $i++) {
$value->text = str_replace($regs[0][$i], '<a class="outlink" href="'.$regs[0][$i].'">'.$regs[0][$i].'</a>', $value->text);
}


$data[] = array(
"profile_image_url" => $value->profile_image_url,
"from_user" => $value->from_user,
"text" => $value->text
);
}


$out = array(
"success" => true,
"results" => $data
);


echo json_encode($out);
exit;
}
?>


The success callback is working ok, It is the same code when you search with the searchPanel.

jratcliff
22 Aug 2011, 8:15 AM
Ahh, I see the problem. The setInterval can't find your gridRefresh function because your gridRefresh function isn't defined globally. But that's ok, just move your setInterval to after your gridRefresh function and don't surround the function name in parens.



//var t = setInterval('gridRefresh()',10000); // << remove this
function gridRefresh(){
Ext.Ajax.request({
url: 'jrr-search-twitter.php?act=search&q=earth',
success: function(e) {
var obj = Ext.util.JSON.decode(e.responseText);
var msg = obj.results;
var html = tpl.apply(msg);
resultPanel.update(html);
}
});
}
var t = setInterval(gridRefresh,10000); // << and add this

rolfiee
22 Aug 2011, 2:25 PM
Thanks, I miss that! You solve my problem. Kudos for you! :D