First item to insert into the mobile database isn't added
Asked Answered
C

1

10

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.

enter image description here

On the second load it seems to work just fine with no call into their JayData module for Kendo:

enter image description here

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?

Corelation answered 20/8, 2014 at 13:7 Comment(3)
I'll give you a +1 just for attempting JayData + Kendo. Despite the JayData folks insisting it works, I've never one successfully make a working JayData + Kendo DataSource combination.Backplate
So far I've been able to finally get everything to work ok. It's just this little bug where they're putting in an .apply and I believe that's what's cutting the first item off somehow. If I had to abandon JayData, which I don't want to do as I have a bit of time into it, any other thoughts on mobile data providers and the sort of cross platform functionality?Corelation
possible duplicate of angularjs promise then not called first timeCultivated
T
1

if you are using html5 then you can use webSQL to perform data operations,it provides all functions like select,insert,update on data The Web SQL specification defines an API for storing data in databases that can be queried using a variant of SQL.

you can use like following function I have used it in my Intel XDK mobile app

if you are using html5 then you can use webSQL to perform data operations,it provides all functions like select,insert,update on data 

> The Web SQL specification defines an API for storing data in databases
> that can be queried using a variant of SQL. you can use like following
> function

<!-- begin snippet: js hide: false -->
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function insert(){
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
var msg;
db.transaction(function (tx) {
 var nam = document.getElementById("Tname").value;
 var id = document.getElementById("Tid").value;
 var name2 = "velocity";
  tx.executeSql('CREATE TABLE IF NOT EXISTS APP (id unique, log)');
  tx.executeSql('INSERT INTO APP (id, log) VALUES (?,?)',[id,nam]);
  //tx.executeSql('INSERT INTO LOGS (id, log) VALUES (61,'+name2+')');
  msg = '<p>Log message created and row inserted.</p>';
  document.querySelector('#status').innerHTML =  msg;
});
}
function readdata(){
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
var id = document.getElementById("Tid").value;
db.transaction(function (tx) {
  tx.executeSql('SELECT * FROM APP', [], function (tx, results) {
  console.log("All rows:");
   var len = results.rows.length, i;
   msg = "<p>Found rows: " + len + "</p>";
   document.querySelector('#status').innerHTML +=  msg;
   for (i = 0; i < len; i++){
     msg = "<p><b>Name :-" + results.rows.item(i).log +"<br/>Contact :-" +results.rows.item(i).id + "</b></p>";
     msg = "<p><b>Name :-" + results.rows.item(i).log +"<br/>Contact :-" +results.rows.item(i).id + "</b></p>";
	 //var row = result.rows.item(i);
     //msg = console.log("  " + row.contact + " " + row.nam);
	 document.querySelector('#status').innerHTML +=  msg;
   }
 }, null);
});
}
function ByContact(){
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
var con = document.getElementById("Con").value;
db.transaction(function (tx) {
  tx.executeSql('SELECT * FROM APP WHERE (id LIKE ?);',[con], function (tx, results) {
  console.log("All rows:");
   var len = results.rows.length, i;
   msg = "<p>Found rows: " + len + "</p>";
   document.querySelector('#status').innerHTML +=  msg;
   for (i = 0; i < len; i++){
     msg = "<p><b>Name :-" + results.rows.item(i).log +"<br/>Contact :-" +results.rows.item(i).id + "</b></p>";
     msg = "<p><b>Name :-" + results.rows.item(i).log +"<br/>Contact :-" +results.rows.item(i).id + "</b></p>";
	 //var row = result.rows.item(i);
     //msg = console.log("  " + row.contact + " " + row.nam);
	 document.querySelector('#status').innerHTML +=  msg;
   }
 }, null);
});
}

</script>
</head>
<body style="background-image:url('f.jpg');background-repeat:no-repeat;">
<h1 align="center"><font color="white">Contact Form</font></h1>
    <div  style="color:white">
<table align="center">
<tr>
<td>contact no</td>
<td><input type="text" id="Tid"/></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" id="Tname"/></td>
</tr>
<tr>
<td>
<button id="add" onclick="return insert();">Insert</button>
</td>
<td>
<button onclick="return readdata();" id="read">readdata</button>
</td>
<td>
</td>
</tr>
</table>
<table>
<tr>
    <td>
    <button onclick="return ByContact();" id="GetByContact">GetByContact</button>
    </td>
    <td>
    <input type="text" id="Con"/>
    </td>
</tr>

        </table>
        <div id="status" name="status"><font color="white">Your Data Will Show Here</font></div>
</div>       

    </body>
</html>

you can get more information from https://github.com/ccoenraets/backbone-directory/tree/master/localdb http://www.tutorialspoint.com/html5/html5_web_sql.htm

Trireme answered 30/9, 2014 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.