如何获取我的变量来存储异步函数中包含的另一个变量的内容?

时间:2014-04-12 06:39:32

标签: javascript angularjs

我整个星期都遇到了这个问题,所以如果有人可以帮助我,我会非常感激。我正在使用角度js,我的代码获取远程json,然后将其存储到一个名为Jsonvar的变量中,然后我使用html5sql库将其连接到一个sqlite数据库中。我有一个自调用函数,它运行代码来查询数据库并将返回的结果存储在一个名为finalVar的变量中。问题是,我需要我的$ scope变量来存储finalVar,它包含保存到数据库的JSON。我非常感谢你的帮助。这是我的代码:

var showApp = angular.module('showApp', ['hmTouchevents']);

showApp.controller('IndexCtrl', function ($scope, $http) {

    $scope.open = function (id) {
        webView = new steroids.views.WebView("/views/show/show.html?id=" + id);
        steroids.layers.push(webView);
    };

    function getStuff2(num) {
        $http.jsonp('http://whateverorigin.org/get?url=' + encodeURIComponent('http://www.catholic.com/api-radio/' + num) + '&callback=JSON_CALLBACK')
            .success(function (response) {
                result = angular.fromJson(response.contents);

                //This must be called to open the connection to the database before any statements are processed
                html5sql.openDatabase("exampleDB", "Example Database", 3 * 1024 * 1024);

                $(function () {
                    var successfullStatements = $("#successfullStatements"),
                        errors = $("#errors");
                    Jsonvar = result.shows;
                    //alert(Jsonvar);
                    html5sql.process(
                        [
                            "DROP TABLE IF EXISTS StarWarsCharacters;",
                            "CREATE TABLE IF NOT EXISTS StarWarsCharacters (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);",
                            "INSERT INTO StarWarsCharacters (name) VALUES ('" + Jsonvar + "');",
                            "INSERT INTO StarWarsCharacters (name) VALUES ('Darth Vader');",
                            "INSERT INTO StarWarsCharacters (name) VALUES ('Luke Skywalker');",
                            "INSERT INTO StarWarsCharacters (name) VALUES ('Princess Leia');",
                        ],
                        function () {

                            //alert('great!');
                        },
                        function (error, statement) {
                            errors.append("<li>" + error.message + " Occured while processing: " + statement + "</li>");
                        }
                    );
                });

                $(function () {
                    var successfullStatements = $("#successfullStatements"),
                        errors = $("#errors")

                        html5sql.process(
                            [{
                                sql: "SELECT * FROM StarWarsCharacters WHERE name=?;",
                                data: [Jsonvar]

                            }],
                            function (transaction, results, rowsArray) {
                                for (var i = 0; i < rowsArray.length; i++) {
                                    finalVar = rowsArray[i].name;
                                }

                            },
                            function (error, statement) {

                            }
                        );
                });

                $scope.shows = finalVar;
                //getlist();

            });
        steroids.view.navigationBar.show("Shows");
        id: "show_id"
    };

    function getStuff1() {
        $.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent('http://www.catholic.com/api-radio/latest') + '&callback=?', function (data) {

            var obj = data.contents;
            obj = JSON.parse(obj);
            var latestnum = obj.latest;
            //alert('works');
            var numbers = new Array(50);
            for (i = 0; i < 50; i++) {
                x = latestnum - i;
                numbers[i] = x;
                //alert(numbers);
            }
            getStuff2(numbers);
            return data.contents;
        });
    }
    getStuff1();
});
// Show: http://localhost/views/show/show.html?id=<id>

showApp.controller('ShowCtrl', function ($scope, $filter, $http) {

    // Fetch all objects from the local JSON (see app/models/show.js)
    (function (shows) {
        // Then select the one based on the view's id query parameter
        $scope.show = $filter('filter')(shows, {
            show_id: steroids.view.params['id']
        })[0];
    });

    // -- Native navigation
    steroids.view.navigationBar.show("Show: " + steroids.view.params.id);

});

0 个答案:

没有答案