工厂申报与Coffeescript在Angular

时间:2014-12-15 21:02:54

标签: angularjs coffeescript

快速提问。我使用ng-classify和Angular和Coffeescript为我的应用程序和我定义工厂。

class DatabaseFactory extends Factory 
  constructor: ($q, DBCONFIG) ->
    return {

      init: () ->
        @db = window.openDatabase(DBCONFIG.name, "1.0", "database", -1)
        angular.forEach DBCONFIG.tables, (table) ->
          columns = []
          angular.forEach table.columns, (column) ->
            columns.push column.name + " " + column.type
            return
          query = "CREATE TABLE IF NOT EXISTS " + table.name + " (" + columns.join(",") + ")"
          @query query
          console.log "Table " + table.name + " initialized"
          return

      query: (query, bindings) ->
        bindings = (if typeof bindings isnt "undefined" then bindings else [])
        deferred = $q.defer()
        @db.transaction (transaction) ->
          transaction.executeSql query, bindings, ((transaction, result) ->
            deferred.resolve result
            return
          ), (transaction, error) ->
            deferred.reject error
            return

          return

        deferred.promise

    }

我正在学习Coffeescript而我无法在第@query query行解决问题

Uncaught TypeError: Cannot read property 'query' of undefined

我尝试了几种方式。如果我使用普通JS,一切都还可以。

angular.module('myApp.services', ['myApp.config'])
// DB wrapper
.factory('DB', function($q, DB_CONFIG) {
    var self = this;
    self.db = null;

    self.init = function() {
        // Use self.db = window.sqlitePlugin.openDatabase({name: DB_CONFIG.name}); in production
        self.db = window.openDatabase(DB_CONFIG.name, '1.0', 'database', -1);

        angular.forEach(DB_CONFIG.tables, function(table) {
            var columns = [];

            angular.forEach(table.columns, function(column) {
                columns.push(column.name + ' ' + column.type);
            });

            var query = 'CREATE TABLE IF NOT EXISTS ' + table.name + ' (' + columns.join(',') + ')';
            self.query(query);
            console.log('Table ' + table.name + ' initialized');
        });
    };

    self.query = function(query, bindings) {
        bindings = typeof bindings !== 'undefined' ? bindings : [];
        var deferred = $q.defer();

        self.db.transaction(function(transaction) {
            transaction.executeSql(query, bindings, function(transaction, result) {
                deferred.resolve(result);
            }, function(transaction, error) {
                deferred.reject(error);
            });
        });

        return deferred.promise;
    };

    return self;
})

1 个答案:

答案 0 :(得分:0)

构造函数不应返回包含方法的对象。在类本身上定义它们,类似于:

class DatabaseFactory extends Factory 
    constructor: (@$q, @DBCONFIG) ->

    init: () ->
        @db = window.openDatabase(@DBCONFIG.name, "1.0", "database", -1)
        angular.forEach @DBCONFIG.tables, (table) ->
            columns = []
            angular.forEach table.columns, (column) ->
                columns.push column.name + " " + column.type
                return
        query = "CREATE TABLE IF NOT EXISTS " + table.name + " (" + columns.join(",") + ")"
        @query query
        console.log "Table " + table.name + " initialized"
        return

    query: (query, bindings) ->
        bindings = (if typeof bindings isnt "undefined" then bindings else [])
        deferred = @$q.defer()
        @db.transaction (transaction) ->
            transaction.executeSql query, bindings, ((transaction, result) ->
                deferred.resolve result
                return
            ), (transaction, error) ->
                deferred.reject error
                return
            return
        deferred.promise

然后this(CoffeeScript中的@)实际上会指向您的类实例。在您的代码中,没有在类上定义query函数(它只是您要返回的匿名对象的一部分),因此@query query失败。实际上,@似乎在您的对象中未定义。