2012-11-07 173 views
4

我想找到一種方法來與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,它實際上可以引用子模型,但我不知道這是否會工作,因爲我無法超越循環依賴問題。

任何幫助,非常感謝。謝謝!

回答

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(); 
    }); 
}); 

我的變化包括在課堂/ Person.js文件中刪除學生和教授的依賴和感動他們進入教室/ PersonCollection.js文件。

之後,我遇到了一個Backbone-relational錯誤,其中addModelScope有一個子模型和名稱空間的錯誤,但是我修復了它,並提出了一個請求:https://github.com/PaulUithol/Backbone-relational/pull/211,這已被接受。

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