【问题标题】:Return local variable in one function to another将一个函数中的局部变量返回给另一个函数
【发布时间】:2013-10-04 06:50:14
【问题描述】:

我正在使用 Angular 构建一个离线 HTML 页面,并使用 ydn-db 进行离线存储。

我有一个这样的数据库服务,

demoApp.SericeFactory.database  = function database() {
    var database = {
        dataStore: null,
        admins: [],
        students: [],
        errors: [],
        getadmindata: function(username) {
           self = null, that = this 
           database.dataStore.get('admins', username).done(function(record) {
                that.self = record;
                return record;
           }).fail(function(e) {
                console.log(e);
                database.errors.push(e);
           });
           return self; //This does not change.
        }
     };

   database.dataStore = new ydn.db.Storage('DemoApp');

   angular.forEach(INITSTUDENTS, function(student) {
       database.dataStore.put('students', student, student.matricno);
    database.students.push(student);
   });

   angular.forEach(INITADMINS, function(admin) {
   database.dataStore.put('admins', admin, admin.username);
    database.admins.push(admin);
   });

   return database;

我还有一个控制器试图使用数据库;

function AppCntl ($scope, database) {
     var user = database.getadmindata('user'); //I get nothing here.
}

我尝试过的, 我尝试将self 更改为var self 我试过像这样拆分函数

rq = database.dataStore.get('admins', 'user');
rq.done(function(record), {
   self = record;
   alert(self.name) //Works.
});
   alert(self) //Doesn't work.

我在 StackOverflow 上遇到过类似的问题,但似乎没有什么对我有用,或者我只是找错地方了。

【问题讨论】:

    标签: javascript angularjs scope angularjs-service ydn-db


    【解决方案1】:

    数据库请求是异步的,因此它会在代码执行结束后稍后执行。

    所以当最后一个alert 执行时,self 仍然是未定义的。第二个alert在db请求完成后执行,通常是正确的设计模式。

    编辑:

    我用下面的代码成功了:

    // Database service
    angular.module('myApp.services', [])
      .factory('database', function() {
        return new ydn.db.Storage('feature-matrix', schema);
      }
    });
    
    // controller using database service
    angular.module('myApp.controllers', [])
     .controller('HomeCtrl', ['$scope', 'utils', 'database', function($scope, utils, db) {
      var index_name = 'platform, browser';
      var key_range = null;
      var limit = 200;
      var offset = 0;
      var reverse = false;
      var unique = true;
      db.keys('ydn-db-meta', index_name, key_range, limit, offset, reverse, unique)
          .then(function(keys) {
            var req = db.values('ydn-db', keys);
            req.then(function(json) {
              $scope.results = utils.processResult(json);
              $scope.$apply();
            }, function(e) {
              throw e;
            }, this);
          });
    }])
    

    完整的应用程序可在https://github.com/yathit/feature-matrix获得

    运行演示应用在这里:http://dev.yathit.com/demo/feature-matrix/index.html

    【讨论】:

    • 谢谢。我必须检查使用 if(!$scope.$$phase){$scope.$digest();} 没有工作。我返回了数据库,然后检查它是否在范围内完成。
    猜你喜欢
    • 2012-05-21
    • 2017-10-20
    • 1970-01-01
    • 2014-01-12
    • 2015-05-06
    • 2019-03-30
    • 2023-02-26
    • 2017-03-26
    • 1970-01-01
    相关资源
    最近更新 更多