gpt4 book ai didi

debugging - 在 CoffeeScript 中使用 Backbone-relational

转载 作者:行者123 更新时间:2023-12-01 06:40:43 25 4
gpt4 key购买 nike

我正在尝试在项目中使用 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 .但是为什么我不能只引用局部函数作用域中的类呢?

最佳答案

But why can't I just refer to the classes in the local function scope?



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

The key to understanding design patterns such as immediately-invoked function expressions is to realize JavaScript has function scope (but not block scope) and passes values by reference inside a closure.



引用文献
  • Immediately Invoked Function Expressions
  • 关于debugging - 在 CoffeeScript 中使用 Backbone-relational,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10685504/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com