【问题标题】:ionic framework with SQL lite带有 SQLite 的离子框架
【发布时间】:2015-05-14 06:49:41
【问题描述】:

我正在使用离子框架。

我正在执行以下步骤

1) 当我的应用程序运行时。它将创建数据库和表 2)一旦使用登录,我将数据保存到mysql并转到仪表板页面

dbQuery.insertCategory();
 $state.go('app.dashboard');

dbQuery 是工厂。

.factory('dbQuery', function ($http, $q, $cordovaSQLite, localstorage) {
     return {
         insertCategory: function () {
             $http({
                 url: "http://mydoman.comcategory",
                 method: 'GET',
                 withCredentials: true,
             }).success((function (result) {
                 var user_id = localstorage.get('user_id');
                 var query = "INSERT OR IGNORE INTO categories (category_id, user_id, category_name,category_type) VALUES (?,?,?,?)";
                 var data = '';
                 result.forEach(function (category) {
                     $cordovaSQLite.execute(db, query, [category.id,user_id, category.category_name, category.category_type]).then(function (res) {
                         console.log("insertId");
                     }, function (err) {

                         console.dir(err);
                     });
                 });
            }))
         },

效果很好

在仪表板上,我正在显示类别列表,但我什么也没找到。

我进行了调试,发现插入类别需要时间。

有没有办法做双向数据绑定

谢谢

【问题讨论】:

    标签: angularjs ionic-framework ionic


    【解决方案1】:

    为什么不使用promise,只在成功promise 触发后重定向到app.dashboard

    例子:

    dbQuery.insertCategory().then(function(result) {
        $state.go('app.dashboard');
    }, function(error) {
        console.error(error);
    });
    

    然后在你的工厂里你可以有这样的东西:

    .factory('dbQuery', function ($http, $q, $cordovaSQLite, localstorage) {
         return {
             insertCategory: function () {
                 var deferred = $q.defer();
                 $http({
                     url: "http://mydoman.comcategory",
                     method: 'GET',
                     withCredentials: true,
                 }).success((function (result) {
                     var user_id = localstorage.get('user_id');
                     var query = "INSERT OR IGNORE INTO categories (category_id, user_id, category_name,category_type) VALUES (?,?,?,?)";
                     var data = '';
                     result.forEach(function (category) {
                         isWaiting = true;
                         $cordovaSQLite.execute(db, query, [category.id,user_id, category.category_name, category.category_type]).then(function (res) {
                             console.log("insertId");
                             isWaiting = false;
                         }, function (err) {
                             console.dir(err);
                             isWaiting = false;
                         });
                         while(isWaiting) {}
                     });
                     deferred.resolve("done");
                }))
                return deferred.promise;
             },
    

    我所做的很多只是理论,但它应该给你一个想法。我创建了一个布尔值来使 SQL 命令同步。由于它们是同步的,当循环结束时,您可以返回您的成功承诺。

    您可能还有其他几种方法可以做到这一点。比如可能将 SQL 的东西移到它自己的函数中并递归地输入它。

    如果这对你有用,请告诉我。

    【讨论】:

    • 效果很好,但是你能解释一下为什么你使用 isWaiting = false;还有这个 while(isWaiting) {} 有什么作用?
    • $cordovaSQLite.execute 是异步的,因此通过循环,您将有 x 异步调用,并立即从 insertCategory 返回,即使它们没有完成。通过添加该循环,在当前 SQLite 函数完成之前,它不会移动到下一个 for 循环迭代。至少理论上它会这样做,因为我没有测试它。当我们确定所有 SQLite 调用/for-loop 迭代都完成时,然后解决承诺,因为我们确定我们确实完成了。有意义吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 2014-06-24
    • 2016-10-03
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多