4

我正在尝试找到一种将骨干关系子模型与 RequireJS 一起使用的方法,其中子模型与超模位于不同的文件中。

例如:

// app.js
define(function() {
  var app = {};
  var app.ns = {};
  Backbone.Relational.store.addModelScope(app.ns);
});

// Classroom/Person.js
define(["app", "./Student", "./Professor"], function(app) {
  app.ns.Classroom.Person = Backbone.RelationalModel.extend({
    subModelTypes: {
      'Student': 'Classroom.Student'
      'Professor': 'Classroom.Professor'
    },

    alertHello: function() {}
  });

  return app.ns.Classroom.Person;
});

// Classroom/Student.js
define(["require", "app", "./Person"], function(require, app) {
  app.ns.Classroom.Student = require("./Person").extend({
    alertHello: function() {
      alert("Hello, I am student " + this.get("name"));
    }
  });

  return app.ns.Classroom.Student;
});

// Classroom/PersonCollection.js
define(["app", "./Person"], function (app, Person) {
  app.ns.Classroom.PersonCollection = Backbone.Collection.extend({
    model: Person
  });
});

// alertPeople.js
define(["Classroom/PersonCollection"], function(PersonCollection) {
  var people = PersonCollection([{"name": "Bob", "type": "Person"}]);
  people.each(function(person) {
    person.alertHello();
  });
});

当我执行 alertPeople.js 时,我期待“你好,我是学生 Bob”的警报,但我收到了这个错误:Uncaught Error: Module name "Classroom/Person" has not been loaded yet for context: _由于我的循环依赖。解决这个问题的可能解决方案是什么?

我的下一个问题可能与 Backbone-relational 的范围有关。在我的 app.js 文件中,我添加了 app.ns 范围的一部分,并确保我的所有模型都在 app.ns 对象中定义。我这样做是因为当我定义我的 subModelTypes 时,它实际上可以引用子模型,但我不确定这是否可行,因为我无法解决循环依赖问题。

任何帮助是极大的赞赏。谢谢!

4

1 回答 1

5

我最终解决了这个问题,我希望这对未来的其他人有所帮助。

首先,我将代码修改为以下内容:

// app.js
define(function() {
  var app = {};
  var app.ns = {};
  Backbone.Relational.store.addModelScope(app.ns);
});

// Classroom/Person.js
define(["app"], function(app) {
  app.ns.Classroom.Person = Backbone.RelationalModel.extend({
    subModelTypes: {
      'Student': 'Classroom.Student'
      'Professor': 'Classroom.Professor'
    },

    alertHello: function() {}
  });

  return app.ns.Classroom.Person;
});

// Classroom/Student.js
define(["app", "./Person"], function(app, Person) {
  app.ns.Classroom.Student = Person.extend({
    alertHello: function() {
      alert("Hello, I am student " + this.get("name"));
    }
  });

  return app.ns.Classroom.Student;
});

// Classroom/PersonCollection.js
define(["app", "./Person", "./Student", "./Professor"], function (app, Person) {
  app.ns.Classroom.PersonCollection = Backbone.Collection.extend({
    model: Person
  });
});

// alertPeople.js
define(["Classroom/PersonCollection"], function(PersonCollection) {
  var people = PersonCollection([{"name": "Bob", "type": "Person"}]);
  people.each(function(person) {
    person.alertHello();
  });
});

我的更改包括删除 Classroom/Person.js 文件中的 Student 和 Professor 依赖项,并将它们移动到 Classroom/PersonCollection.js 文件中。

在此之后,我遇到了 Backbone-relational 的错误,其中 addModelScope 有子模型和命名空间的错误,但我修复了它并提出了拉取请求:https ://github.com/PaulUithol/Backbone-relational/pull/211 ,已被接受。

可以在https://github.com/andrewferk/backbone-relational-submodels找到更完整的示例。

于 2012-11-07T20:47:21.370 回答