不添加要插入移动数据库的第一项

时间:2014-08-20 13:07:35

标签: javascript jquery kendo-ui telerik jaydata

我在Telerik Platform移动应用程序中使用JayData。 JayData的优秀人才起到了我想要做的这个例子:

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();
        };

我已添加到此代码中,当我运行它时也使用他们的Kendo.js模块用于JayData(与kendo的kendo.js不同),它似乎在第一个承诺创建时中断脚本循环功能。这只发生在第一次运行脚本时 - 如果您要刷新以进行重新加载,则它会正确运行并且第一个项目将被插入。

enter image description here

在第二次加载时,似乎工作正常,没有调用他们的JayData模块用于Kendo:

enter image description here

因此,即使它似乎要添加第一个项目(ID 19),该项目也永远不会添加到数据库中。但是,当您重新加载相同的脚本时,它会再次被标记为添加并且不会被中断,因此它最终会进入数据库。

任何人都有任何想法或事情要尝试?

1 个答案:

答案 0 :(得分:1)

如果您使用的是html5,那么您可以使用webSQL执行数据操作,它提供所有功能,如select,insert,update on data Web SQL规范定义了一个API,用于在可以使用SQL变体查询的数据库中存储数据。

您可以使用我在英特尔XDK移动应用程序中使用的以下功能

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>

您可以从中获取更多信息 https://github.com/ccoenraets/backbone-directory/tree/master/localdb http://www.tutorialspoint.com/html5/html5_web_sql.htm