简体   繁体   English

如何使用YDN-DB在另一个HTML页面中加载数据库上下文?

[英]How to load a db context in another Html page using YDN-DB?

I've loaded all my data on the very first page: 我已经在第一页上加载了所有数据:

<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> 
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script src="/Scripts/ydn.db-jquery-0.7.5.js" type="text/javascript"></script>
<script src="/Scripts/jquery.mousewheel.js"></script>
...
var schema = {
        stores: [{
            name: 'products',
            keyPath: 'cdProduto',
            autoIncrement: false,
            indexes: [
                {
                    keyPath: 'cdCategoria'
                }, {
                    keyPath: 'dtUltimaAtualizacao'
                }]
        }]
    };

    var db = new ydn.db.Storage('db-test', schema);

    db.clear().done(function (num) {            
        db.add('products', [<%=jsonProducts%>]);            
    });

The <%=jsonProducts%> prints some json from C# back-end. <%= jsonProducts%>从C#后端打印一些json。

I can load the information like this, it works: 我可以像这样加载信息,它的工作原理是:

db.get('products', '2').always(function (record) {...

After, in the same browser and session, when I try to load my second page (/catalogo.html), then load the same product with the '2' key, like this: 之后,在同一浏览器和会话中,当我尝试加载第二个页面(/catalogo.html)时,然后使用“ 2”键加载相同的产品,如下所示:

<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> 
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script src="/Scripts/ydn.db-jquery-0.7.5.js" type="text/javascript"></script>
<script src="/Scripts/jquery.mousewheel.js"></script>
...
var db = new ydn.db.Storage('db-test');         
db.get('products', '2').done(function (value) {
    console.log(value);
});

It returns 'undefined' on the console. 它在控制台上返回“ undefined”。 I have no clues. 我没有头绪

Besides, im using html cache manifest on both pages 此外,我在两个页面上都使用html缓存清单

<html manifest="/home/manifest">

and testing under local server, acessing with localhost and port 60873, for example. 并在本地服务器下进行测试,例如使用本地主机和端口60873进行访问。

Am I missing something? 我想念什么吗? Ain't that the right way to open an existing YDN-DB? 这不是打开现有YDN-DB的正确方法吗?

Thanks! 谢谢!

You are not missing anything and it should work. 您不会丢失任何东西,它应该可以工作。 You should check indexeddb content on resource panel of dev console. 您应该在开发控制台的资源面板上检查indexeddb内容。

You should initialize in second page /catalogo.html exactly as in first page, ie, 您应该在第二页/catalogo.html中进行与第一页完全相同的初始化,即

var db = new ydn.db.Storage('db-test', schema);

Changing schema my drop some tables. 更改架构可以删除一些表。 Such data lost occur when database constraint are not met. 当不满足数据库约束时,会发生此类数据丢失。 It could be triggered by the library or browser itself. 它可以由库或浏览器本身触发。

It is a good practice to check database connection before using as follow: 优良作法是在使用数据库之前先检查数据库连接,如下所示:

db.addEventListener('ready', function (event) {
  var is_updated = event.getVersion() != event.getOldVersion();
  if (is_updated) {
    console.log('database connected with new schema');
  } else if (isNaN(event.getOldVersion()))  {
    console.log('new database created');
  } else {
    console.log('existing database connected');
  }
  // heavy database operations should start from this.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM