我想找到一種方法來與RequireJS一起使用Backbone-relational子模型,其中子模型與超級模型在不同的文件中。與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,它實際上可以引用子模型,但我不知道這是否會工作,因爲我無法超越循環依賴問題。
任何幫助,非常感謝。謝謝!