0

我正在尝试编写一个咖啡脚本类,当从中构造一个新对象时,它会检查是否传递了一个 ID。如果是这样,请尝试查找匹配的文档并从中填充对象。如果没有传递 ID,则生成一个新 ID 并创建一个新文档。我mongojs用来连接到我的 mongodb。但是,当我从 TestObject 类创建一个新对象时,它会引发一个错误,即集合名称需要是一个字符串。我在该类上将@collection 设置为字符串,因此我控制台记录了@collection 属性及其未定义。这里发生了什么,我怎样才能使这项工作?

class MongoObject
    @collection
    constructor: (id) ->
        @_id = if typeof id is 'undefined' then require('node-uuid').v4() else id
        @db = require('mongojs') config.mongo_server, [@collection]

        @db[@collection].findOne
            _id: @_id,
            (error, story) ->
                # Matching document found. Import data from document
                if not error and story
                    for field, value of story
                        @[field] = value if field is not '_id'

                # Matching document not found. Creating new one
                if not error and not story
                    @db[@collection].save
                        id: @id

                # Error occured
                if error and not story
                    console.error error

                return

class TestObject extends MongoObject
    @collection = 'TestObjects'
    constructor: (id) ->
        super('TestObject')

编辑

重读我的代码,很明显这是构造函数和 @collection 在 MongoObject 中未定义的问题。有没有更好的方法来做到这一点?我可以创建一个setupDB方法并在每个类的构造函数中调用它,该构造函数在超级调用之后扩展 MongoObject,但不是我所希望的。

编辑 2

我修改了我的代码。但是,我现在收到一个constructor未定义的错误。当我查看已编译的 javascript 时,它指向constructor;MongoObject 代码的顶部。奇怪的是,coffeescript 没有放var constructor;,这通常是发生的。我发布了翻译后的 javascript 仅供参考

咖啡脚本

class MongoObject
    collection: undefined
    constructor: (id) ->
        @_id = if typeof id is 'undefined' then require('node-uuid').v4() else id
        @db = require('mongojs') config.mongo_server, [@collection]

        @db[@collection].findOne
            _id: @_id,
            (error, story) ->
                # Matching document found. Import data from document
                if not error and story
                    for field, value of story
                        @[field] = value if field is not '_id'

                # Matching document not found. Creating new one
                if not error and not story
                    @db[@collection].save
                        id: @id

                # Error occured
                if error and not story
                    console.error error

                return

class TestObject extends MongoObject
    collection = 'TestObjects'
    constructor: (id) ->
        super('TestObject')

Javascript

MongoObject = (function() {
  MongoObject.prototype.collection = void 0;

  function MongoObject(id) {
    this._id = typeof id === 'undefined' ? require('node-uuid').v4() : id;
    this.db = require('mongojs')(config.mongo_server, [this.collection]);
    this.db[this.collection].findOne({
      _id: this._id
    }, function(error, story) {
      var field, value;
      if (!error && story) {
        for (field in story) {
          value = story[field];
          if (field === !'_id') {
            this[field] = value;
          }
        }
      }
      if (!error && !story) {
        this.db[this.collection].save({
          id: this.id
        });
      }
      if (error && !story) {
        console.error(error);
      }
    });
  }

  return MongoObject;

})();

TestObject = (function(_super) {
  var collection;

  __extends(TestObject, _super);

  collection = 'TestObjects';

  function TestObject(id) {
    TestObject.__super__.constructor.call(this, 'TestObject');
  }

  return TestObject;

})(MongoObject);

编辑 3

根据我的评论更新了我的代码。它的说法@constructor.collection是未定义的

@db[@constructor.collection].save
    id: @id

我假设它是因为它在保存的回调函数中。前进一步,后退两步。

修改后的代码

class MongoObject
    @collection
    constructor: (id) ->
        @_id = if typeof id is 'undefined' then require('node-uuid').v4() else id
        @db = require('mongojs') config.mongo_server, [@constructor.collection]

        @db[@constructor.collection].findOne
            _id: @_id,
            (error, story) ->
                # Matching document found. Import data from document
                if not error and story
                    for field, value of story
                        @[field] = value if field is not '_id'

                # Matching document not found. Creating new one
                if not error and not story
                    @db[@constructor.collection].save
                        id: @id

                # Error occured
                if error and not story
                    console.error error

                return

class TestObject extends MongoObject
    @collection: 'TestObjects'
    constructor: (id) ->
        super('TestObject')
4

3 回答 3

1

我认为您@对班级级别的含义感到困惑。一个简化的示例应该会有所帮助,这个 CoffeeScript:

class B
    @p: 'b'

与此 JavaScript 相同:

var B = (function() {
  function B() {}
  B.p = 'b';
  return B;
})();

所以你可以看到这p是一个直接附加到C类/函数的类属性。但是,当您在方法内部时,例如constructor@引用实例,因此,在您的情况下,@collection将是undefined因为您将其定义collection为类属性。

您可能想collection成为一个实例属性:

class MongoObject
    collection: undefined
    constructor: (id) ->
        # @collection is what you expect it to be in here

class TextObject extends MongoObject
    collection: 'TextObject'

演示:http: //jsfiddle.net/ambiguous/TzK5E/

或者,您可以保留collection为类属性并通过以下方式引用它@constructor

class MongoObject
    @collection: undefined
    constructor: (id) ->
        # @constructor.collection is what you expect it to be in here

class TextObject extends MongoObject
    @collection: 'TextObject'

演示:http: //jsfiddle.net/ambiguous/wLjz3/

于 2013-08-26T02:36:45.313 回答
0

我相信您会希望使用稍微不同的语法来引用collection名称:

class MongoObject
    @collection 
    constructor: (id) ->
       alert @constructor.collection


class TestObject extends MongoObject
    @collection = 'TestObjects'
    constructor: (id) ->
        super('TestObject')

t = new TestObject

警报"TestObjects"

关键是使用@constructor.collection

于 2013-08-26T02:41:24.720 回答
0

这是我最终得到的解决方案

class MongoObject
    @collection
    constructor: (id) ->
        @_id = if typeof id is 'undefined' then require('node-uuid').v4() else id
        @db = require('mongojs') config.mongo_server, [@constructor.collection]

        @db[@constructor.collection].findOne
            _id: @_id,
            (error, doc) =>
                # Matching document found. Import data from document
                if not error and doc
                    console.log 'Document found. Updating.'
                    for field, value of doc
                        @[field] = value if field is not '_id'

                # Matching document not found. Creating new one
                if not error and not doc
                    console.log 'No document found. Creating.'
                    @db[@constructor.collection].save
                        _id: @_id

                # Error occured
                if error and not doc
                    console.error error

                return

class TestObject extends MongoObject
    @collection: 'TestObjects'
于 2013-08-26T04:20:36.007 回答