使用Backbone-relational与CoffeeScript

时间:2012-05-21 12:43:42

标签: debugging scope coffeescript backbone-relational

我正在尝试在项目中使用Backbone-relational和CoffeeScript。以下是CoffeeScript中我正在尝试建模的事物类型的示例:

  class NestedModel extends Backbone.RelationalModel
    defaults:
     Description: 'A nested model'

  NestedModel.setup()

  class MainModel extends Backbone.RelationalModel
    defaults:
     Description: 'A MainModel description'
     StartDate: null

    relations: [
      type: Backbone.HasOne
      key:  'nestedmodel'
      relatedModel: 'NestedModel'
      includeInJSON: '_id'
      reverseRelation:
        type: Backbone.HasOne
        includeInJSON: '_id'
        key: 'mainmodel'   
    ]

  MainModel.setup()     

  nm = new NestedModel()
  mm = new MainModel(nestedmodel: nm)
  console.log mm.get("nestedmodel").get("mainmodel").get("Description")
  return 

CoffeeScript生成以下JavaScript:

  var MainModel, NestedModel, mm, nm;
  var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };
  NestedModel = (function() {
    __extends(NestedModel, Backbone.RelationalModel);
    function NestedModel() {
      NestedModel.__super__.constructor.apply(this, arguments);
    }
    NestedModel.prototype.defaults = {
      Description: 'A nested model'
    };
    return NestedModel;
  })();
  NestedModel.setup();
  MainModel = (function() {
    __extends(MainModel, Backbone.RelationalModel);
    function MainModel() {
      MainModel.__super__.constructor.apply(this, arguments);
    }
    MainModel.prototype.defaults = {
      Description: 'A MainModel description',
      StartDate: null
    };
    MainModel.prototype.relations = [
      {
        type: Backbone.HasOne,
        key: 'nestedmodel',
        relatedModel: 'NestedModel',
        includeInJSON: '_id',
        reverseRelation: {
          type: Backbone.HasOne,
          includeInJSON: '_id',
          key: 'mainmodel'
        }
      }
    ];
    return MainModel;
  })();
  MainModel.setup();
  nm = new NestedModel();
  mm = new MainModel({
    nestedmodel: nm
  });
  console.log(mm.get("nestedmodel").get("mainmodel").get("Description"));
  return;

会产生以下警告和错误

Warning:
Relation= child
; no model, key or relatedModel (function MainModel() {
              MainModel.__super__.constructor.apply(this, arguments);
            }, "nestedmodel", undefined)


Error:
Uncaught TypeError: Cannot call method 'get' of undefined

只需从生成的JavaScript的第一行中删除'NestedModel'变量

var MainModel, NestedModel, mm, nm;

导致正确的行为。显然,我不能继续从生成的JavaScript中删除变量定义。我做错了什么?

好的,这似乎是一个范围问题。请参阅以下jsFiddle example。但为什么我不能只引用本地函数范围中的类?

1 个答案:

答案 0 :(得分:0)

  

但为什么我不能只引用本地函数范围中的类?

类实现为立即调用的函数表达式

  

理解设计模式(如立即调用的函数表达式)的关键是实现JavaScript具有函数作用域(但不是块作用域),并通过引用在闭包内传递值。

<强>参考