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

前端之家收集整理的这篇文章主要介绍了javascript – 不添加要插入移动数据库的第一项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在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.js从kendo),似乎中断脚本当第一个承诺在循环函数中创建.这只会发生在脚本运行的第一个时间 – 如果要刷新来重新加载,则它会正常运行,并且第一个项目被插入.

在第二个负载上,似乎工作正常,没有调用他们的JayData模块Kendo:

所以即使它似乎添加第一个项目(ID 19),该项目也不会被添加数据库.当您重新加载相同的确切脚本时,它会被再次标记添加,并且不会中断,所以最终进入数据库.

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

解决方法

如果你使用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,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',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',2 * 1024 * 1024);
var con = document.getElementById("Con").value;
db.transaction(function (tx) {
  tx.executesql('SELECT * FROM APP WHERE (id LIKE ?);',[con],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

猜你在找的JavaScript相关文章