I'm using JayData in a Telerik Platform mobile application. The good people at JayData worked up this example of what I was looking to do:
http://jsfiddle.net/JayData/zLV7L/
var savefeedIfNotExists = function (feed) {
//create jQuery promise
console.log("create deferred for " + feed.FeedID)
var def = new $.Deferred();
//async thread
pnrDB.PNRFeeds.filter('it.FeedId == ' + feed.FeedID).count(function (count) {
console.log("Add Feed - " + feed.FeedName);
if (count == 0) {
var f = new PNRFeed({
FeedId: feed.FeedID,
FeedName: feed.FeedName,
ImageName: feed.ImageName,
FeedActive: feed.IsActive,
OrderNumber: parseInt(feed.OrderNumber) + 1
})
pnrDB.PNRFeeds.add(f);
console.log("Resolve for - " + feed.FeedName);
//promise.resolve() indicates that all async operations have finished
//we add the ADD/SKIP debug info to the promise
def.resolve("ADD");
console.log("Resolved - " + feed.FeedName);
} else {
//console.log('feed id not 0 - ' + f.FeedId);
def.resolve("SKIP");
}
});
//return promise in order to wait for the async result of local database query
return def.promise();
};
I've added to this code however and when I run it also using their Kendo.js module for JayData (different than the kendo.js from kendo), it seems to interrupt the script when the first promise is created in the loop function. This only happens the FIRST time the script is run - if you were to refresh to do a reload, it then runs correctly and the first item DOES get inserted.
On the second load it seems to work just fine with no call into their JayData module for Kendo:
So even though it appears as though it's going to add the first item (ID 19), that item never gets added to the database. When you reload the same exact script however, it gets tagged as add again and isn't interrupted, so it finally gets into the database.
Anyone have any thoughts or things to try?