Ionic ngCordova SQLite 执行方法 - 调用 'then' 后应返回控件



我有一个函数'insertAllLinksFunc'-在数据库中插入数据。当execute方法在insertAllLinksFunc中被调用时,控件在'then'方法完成之前返回。我希望控件只在'then'方法完成后返回。

日志:

            8     697189   log      test : inside test              
            9     697193   log      insertAllLinksFunc : inside insertAllLinks : arr length=1               
            10    697195   log      insertAllLinksFunc : ret=               
            11    697196   log      test : arr0=                
            12    697197   log      test : exitting test                
            13    697206   log      insertAllLinksFunc : item=LinkEntity[linkId=1443150697190, title=title-1443150697190, imgPath=imgPath-1443150697190]

我想要的是log 13代码应该在log 9和log 10代码之间执行

代码:

myController.js

            var db=null;
            var myModule = angular.module('MyController', ['ionic', 'ngCordova'])
            .run(function($ionicPlatform, $cordovaSQLite) {
              $ionicPlatform.ready(function() {
                console.log('inside onReady');
                db = $cordovaSQLite.openDB("my.db");
                $cordovaSQLite.execute(db, "create table if not exists POSTS_TBL( POST_ID numeric primary key , CREATE_DT text not null, POST_TYPE text not null, TH_LOCAL_IMG_PATH text , TH_SERVER_IMG_PATH text , LINK_URL text , TITLE text , CAPTION text , MESSAGE text , DESCRIPTION text , LOCAL_IMG_PATH text , SERVER_IMG_PATH text , POST_JSON text not null)");
                console.log('exitting onReady, db='+db);
              });
            })
            .factory('queryFactory', function($cordovaSQLite){
insertAllLinksFunc = function(arr){
    console.log('insertAllLinksFunc : inside insertAllLinks : arr length='+arr.length);
    var ret = null;
    if(arr.length>0){
        var query = "INSERT INTO POSTS_TBL (POST_ID, TITLE, CAPTION, POST_JSON, DESCRIPTION, MESSAGE, SERVER_IMG_PATH, LINK_URL, CREATE_DT, POST_TYPE) VALUES (?,?,?,?,?,?,?,?,?,?)";
        var ret = [];
        for(item in arr){
            console.log('insertAllLinksFunc : item='+arr[item].toString());
            $cordovaSQLite.execute(db, query, [arr[item].linkId, arr[item].title, arr[item].caption, arr[item].linkJSON, arr[item].description, arr[item].msg, arr[item].serverPath, arr[item].url, arr[item].createDt, Settings.POST_TYPE_LINK]).then(function(res) {
            console.log("insertAllLinksFunc : insertAllLinksFunc : INSERT ID -> " + res.insertId);
            ret.push(res);
            }, function (err) {
                console.error(err);
                return -1;
            });
        }
        console.log('insertAllLinksFunc : ret=' + ret);
        return ret;
    }
}
            return {
                insertAllLinks : insertAllLinksFunc
            }})

app.js

            angular.module('starter', ['ionic', 'ngCordova', 'MyController'])
            .controller('myCtrl', function($scope, queryFactory){
              $scope.test = function(){
                console.log('test : inside test');    
                var time = new Date().getTime();
                var entity = new LinkEntity(time, 'title-'+time, 'imgPath-'+time, 'serverPath'+time, '{}', 'url', 'caption', 'description', time, 'msg');
                var arr = [entity];
                var arr0 = queryFactory.insertAllLinks(arr);
                console.log('test : arr0='+arr0);
                console.log('test : exitting test');
              }  
            });

entities.js

            function LinkEntity(linkId, title, imgPath, serverPath, linkJSON, url, caption, description, createDt, msg){
                this.linkId = linkId;
                this.title = title;
                this.imgPath = imgPath;
                this.serverPath = serverPath;
                this.linkJSON = linkJSON;
                this.url = url;
                this.caption = caption;
                this.description = description;
                this.createDt = createDt;
                this.msg = msg;
            }
            LinkEntity.prototype.toString = function(){
                return 'LinkEntity[linkId='+ this.linkId +', title='+this.title+', imgPath='+this.imgPath+']';
            }

sql lite函数$cordovaSQLite。Execute是一个异步函数。这意味着当调用函数并插入数据时,应用程序正在工作。如果数据已经插入,则调用".then()"中的函数。所以。then()中的所有代码都是稍后调用的你的应用程序已经编写了第9行&10 .

要理解这个问题,你需要看一下:AngularJS $q and promises.

在你的例子中,你遇到了处理多个承诺的问题。为此,您可以将承诺收集到一个数组中并使用$q。All:组合承诺或stackoverflow: how-to-chain-multiple promises-within-and-after-a-for循环。但我认为,如果您了解如何使用$q和promises,您将了解主要问题。

最新更新