I'm working on this project for learning purposes. The tasks for now are very simple:
- Populate data from DB using
$.getJSON
. - Check every
'n'
seconds for new data andappend
it to the list. - Notify user about new data changes.
Here is the example of where I got so far: ( JSBin /Don't forget to run js) All the issues will be visible when running the example.
Here is the JS code that i have:
$(document).bind('pageinit', function(){
var $myList = $( "#myList" );
var newItems = [];
function loadList(){
$.getJSON("http://jsbin.com/vayeni/2.js",function(data){
$.each(data, function( index, value ) {
newItems.push( "<li><a>" + value.airline + "</a></li>" );
if(data>newItems){
alert('New Entry');
data=newItems;
}
});
$myList.append( newItems.join( "" ) );
$myList.listview( "refresh" );
setTimeout(loadList,1000);
});
}
loadList();
});
Thanks for your help !
Your data comparison is not correct.
You are comapring this:
to this:
There will be always inequality.
You should use another approach. For example, if the
id
field from your JSON array is an unique one you can attach it to each item from the unordered list as anid
attribute. For example:This way, at each iteration you can check if the incomming JSON item already exists into your list and add it when there is no match. Eg:
Finally, you can append the
newItems
contents directly if there are items inside:Here is the edited snippet: JSBin