Web db - 取回数据

发布于 2024-10-11 05:19:42 字数 1166 浏览 2 评论 0原文

这是代码

    onError = function(tx, e) {
        alert('Something unexpected happened: ' + e.message );
    }

    var webdb = openDatabase(dbName, '1.0' , dbDesc , dbSize);

    var colourArray = new Array();

    webdb.transaction(function(tx) {
        tx.executeSql('SELECT * FROM colours', 
                      [], 
                      function(tx,rs) {
                        var ctn = rs.rows.length;
                        for (var i=0; i < ctn; i++) {
                            var row = rs.rows.item(i);                
                            colourArray.push([row.id , row.title]);
                        }   
                      }, 
                      onError);
    });
    /**
     * the array looks like [[1,'red'],[2,'white'],[3,'black'] ...]
     */
     var ColourStore = new Ext.data.ArrayStore({fields: ['key', 'val'],
                                               data: colourArray});

表“颜色”包含颜色名称和哈希代码。它应该由 ExtJS Ext.data.ArrayStore 使用,然后填充大型表单上的其他下拉列表。

我的问题是 - 我无法将数据作为数组取回。变量“colourArray”为空...我知道我遇到了一些 javascript 闭包、循环问题...但只是不知道如何取回内部循环值。尝试多多返回->返回->返回功能和更多返回。它们都不起作用。

Here is the code

    onError = function(tx, e) {
        alert('Something unexpected happened: ' + e.message );
    }

    var webdb = openDatabase(dbName, '1.0' , dbDesc , dbSize);

    var colourArray = new Array();

    webdb.transaction(function(tx) {
        tx.executeSql('SELECT * FROM colours', 
                      [], 
                      function(tx,rs) {
                        var ctn = rs.rows.length;
                        for (var i=0; i < ctn; i++) {
                            var row = rs.rows.item(i);                
                            colourArray.push([row.id , row.title]);
                        }   
                      }, 
                      onError);
    });
    /**
     * the array looks like [[1,'red'],[2,'white'],[3,'black'] ...]
     */
     var ColourStore = new Ext.data.ArrayStore({fields: ['key', 'val'],
                                               data: colourArray});

The table "colours" contain colour name and hash code. And it was suppose to be use by a ExtJS Ext.data.ArrayStore then populate other drop downs on a massive form.

My problem is - I couldn't get the data back as an array. The variable "colourArray" is empty ... I know I hit some javascript closure , loop problem ... but just couldn't figure out how to get that inner loop value back. Try a lot of return -> return -> return function and more return. None of them works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我还不会笑 2024-10-18 05:19:42

executeSQL 是异步的。您需要在 function(tx,rs) 回调函数中创建 ColourStore。如果您需要全局可用的数据,则在executeSQL 调用回调函数之前无法初始化应用程序。例子:

Ext.onReady( function() {
    var webdb = openDatabase(dbName, '1.0' , dbDesc , dbSize);

    var colourArray = new Array();
    var ColourStore;

    webdb.transaction( function(tx) {
        tx.executeSql('SELECT * FROM colours',
        [], function(tx,rs) {
            var ctn = rs.rows.length;
            for (var i=0; i < ctn; i++) {
                var row = rs.rows.item(i);
                colourArray.push([row.id , row.title]);
            }
            //INIT YOUR APP HERE IN ORDER TO HAVE ACCESS TO colourArray values
            ColourStore = new Ext....
            YourApp.init();
        },
        onError);
    });
}, this);

executeSQL is asynchronous. You need to create ColourStore in the function(tx,rs) callback function. If you need the data available globally, you can't init your app until executeSQL calls the callback function. Example:

Ext.onReady( function() {
    var webdb = openDatabase(dbName, '1.0' , dbDesc , dbSize);

    var colourArray = new Array();
    var ColourStore;

    webdb.transaction( function(tx) {
        tx.executeSql('SELECT * FROM colours',
        [], function(tx,rs) {
            var ctn = rs.rows.length;
            for (var i=0; i < ctn; i++) {
                var row = rs.rows.item(i);
                colourArray.push([row.id , row.title]);
            }
            //INIT YOUR APP HERE IN ORDER TO HAVE ACCESS TO colourArray values
            ColourStore = new Ext....
            YourApp.init();
        },
        onError);
    });
}, this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文