ajax - How to compare Web SQL data to fetched json and then delete certain objects if ID's match -


i'm reaching out have experience cordova web sql. if can solve problem thank when project becomes open source through github. i'm fetching json wordpress , i'm rendering posts in phonegap cordova ios app. have made function in index.js called addread stores posts id in local web sql database. part figured out when doing ajax request (with jquery) can render posts want filter out posts have been read, ones in table called readposts.

to give perspective, addread function , table:

createtable: function () {         app.db.transaction(function (tx) {             tx.executesql("create table if not exists readposts(id integer primary key asc, postid integer, added_on text)", [],                 app.onsuccess, app.onerror);         });     },      addread: function (postid) {         app.db.transaction(function (tx) {             var ts = new date().toutcstring();             tx.executesql("insert readposts(postid, added_on) values (?,?)", [postid, ts], app.onsuccess, app.onerror);         });     } 

i'm fetching json here (you don't have through everything, find out posts , ids are): http://eter.rudbeck.info/category/sjalvstudier/?json=1&count=10&apikey=ertyndskatczmuf6

inside ajax request success function i'm looping through readposts table , inside loop going through fetched json. inside if statement comparing postid inside table id fetched json. if match want delete post json.

my code seem logical me , loops work program ignores if statement , still renders posts though table's postid:s matched id:s fetched json. looks inside success function in ajax request (i've commented out render code because not relevant):

success: function(data){                app.db.transaction(function (tx) {                     tx.executesql("select * readposts order id desc", [], function (tx, rs) {                         var row;                         (var = 0; < rs.rows.length; i++) { // loop through web sql db                             row = rs.rows.item(i);                             $.each(data.posts, function(index, obj) { // loop through fetched json                                        if(parseint(row.postid) == parseint(obj.id)) // make sure both values integers {                                     delete obj;                                 }                             });                                 }                     }, app.onerror);                });                 // render code, ignore                /*                var source   = $("#blog-template").html();                var template = handlebars.compile(source);                var blogdata = template(data);                $('#blog-data').html(blogdata);                $('#blog-data').trigger('create');                dfd.resolve(data);                $(".nav1 li a").removeclass('active-menu');                       $('#btn-senaste').addclass("active-menu");                 $('#recommended-data').html('<p></p>');                 */            }, 

both of loops work if statement try ro delete posts ignored. believe piece of code needs fixed.

thank help

i suspect "if" statement may evaluating true, delete statement not having effect. can't delete value object passed jquery's $.each() function; need delete in context of parent object - see this fiddle

so try this:

$.each(data.posts, function(index, obj) { // loop through fetched json            if(parseint(row.postid) == parseint(obj.id)) // make sure both values integers {         delete data.posts[index];     } });  

i'd recommend using step-through debugger place breakpoint on line suspect; way can inspect values of variables , determine what's going on. depending platform you're testing on, if it's android 4.4+ can use chrome remote debugging, ios 6+ safari remote debugging, or ripple emulator plain old chrome dev tools.

hope helps :-)


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -