Using Backbone-Relational with CoffeeScript

I am trying to use Backbone-relational and CoffeeScript in a project. The following is an example in CoffeeScript of the type of thing I'm trying to simulate:

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 

What CoffeeScript creates the following 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; 

What causes the following warning and errors

 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 

Just remove the "NestedModel" variable from the first line of generated JavaScript

 var MainModel, NestedModel, mm, nm; 

Causes the correct behavior. Obviously, I cannot remove the variable definition from the generated JavaScript. What am I doing wrong?

Well, that seems to be the problem. See the following jsFiddle example . But why can't I just refer to classes in the area of ​​local functions?

+4
source share
1 answer

But why can't I just refer to classes in the area of ​​local functions?

Classes are implemented as expressions with expressions

The key to understanding design patterns, such as function calls that are immediately invoked, is to implement JavaScript with a function area (but not a block area) and pass values ​​by reference inside the closure.

References

0
source

Source: https://habr.com/ru/post/1413526/


All Articles