我是BackboneJS的新手,我一直在使用RequireJS的主干-关系模型来处理嵌套关系,-I认为我遇到了循环问题。任何帮助我们都将不胜感激!
我有以下模型和收藏:
/* Module Model at models/module*/
define([
'jquery',
'underscore',
'backbone',
'backboneRelational',
], function($, _, Backbone) {
var ModuleModel = Backbone.RelationalModel.extend({
urlRoot: 'api/module',
_radius: 50,
relations: [{
type: Backbone.HasMany,
key: 'children',
relatedModel: 'ModuleModel',
collectionType: 'ModuleCollection',
reverseRelation: {
key: 'parent_id',
includeInJSON: 'id'
}
}],
url: function() {
return this.id? 'api/module/' + this.id : 'api/module';
}
});
return ModuleModel;
});
/* Module Collection */
define([
'jquery',
'underscore',
'backbone',
'models/module'
], function($, _, Backbone, ModuleModel) {
var ModuleCollection = Backbone.Collection.extend({
model: ModuleModel,
url: 'api/modules'
});
return ModuleCollection;
});当我初始化对象ModuleModel时,它抛出以下错误:
Relation=child; no model, key or relatedModel (function (){ parent.apply(this, arguments); }, "children", undefined)你能告诉我正确的方向吗?
发布于 2012-05-09 16:47:14
这看起来像是作用域问题。在ModuleModel的初始化过程中,它想要创建一个与自身的hasMany关系,但是它找不到自己,它会以上述错误的形式给你带来痛苦:
http://jsfiddle.net/yNLbq
一旦对象可以从当前作用域到达,事情就开始解决了:
http://jsfiddle.net/jDw5e
一种可能的解决方案是为模型和集合本身提供一个可以从当前作用域访问的命名空间。
希望这能有所帮助。
发布于 2013-06-09 09:06:24
我在这里遇到了这个问题:RequireJS + BackboneRelational + Self-Referential。他似乎从这个帖子中继承了一些问题,所以我想我应该加上我的一毛钱。
首先,由于您使用的是RequireJS,所以没有全局变量。您不能简单地提供对象的名称,您需要为relatedModel和collectionType提供实际的对象引用。
你最棘手的问题是ModuleModel的relatedModel实际上是ModuleModel本身,当你将它赋值给relatedModel (使用AMD模型)时,它不会被定义,你必须将赋值推迟到ModuleModel被赋值之后。
最后,您需要解析循环引用。当dokkaebi建议使用exports时,他是正确的,但他的实现实际上滥用了exports。在导出时,按照他的建议将对象直接附加到exports,但在导入时,您需要引用模块才能使用它,而不是exports。
这应该是可行的:
ModuleModel.js
define(['exports', 'ModuleCollection'], function (exports, Module) {
'use strict';
var ModuleModel = Backbone.RelationalModel.extend({
urlRoot: 'api/module',
_radius: 50,
relations: [{
type: Backbone.HasMany,
key: 'children',
// ModuleModel is undefined; this line is useless
// relatedModel: ModuleModel,
// no globals in require; must use imported obj ref
collectionType: Module.Collection,
reverseRelation: {
key: 'parent_id',
includeInJSON: 'id'
}
}],
url: function() {
return this.id? 'api/module/' + this.id : 'api/module';
}
});
// Now that `ModuleModel` is defined, we can supply a valid object reference:
ModuleModel.prototype.relations[0].relatedModel = ModuleModel;
// Attach `Model` to `exports` so an obj ref can be obtained elsewhere
exports.Model = ModuleModel;
});ModuleCollection.js
define(['exports', 'ModuleModel'], function(exports, Module) {
'use strict';
var ModuleCollection = Backbone.Collection.extend({
// must reference the imported Model
model: Module.Model,
url: 'data.php' // <-- or wherever
});
// Attach `Collection` to `exports` so an obj ref can be obtained elsewhere
exports.Collection = ModuleCollection;
});Main.js
define(['ModuleCollection'], function(Module) {
'use strict';
var modules = new Module.Collection();
modules.fetch().done(function() {
modules.each(function(model) {
console.log(model);
});
});
});发布于 2012-06-09 06:26:15
来自backbone-relational.js v0.5.0 (第375行)中的注释:
// 'exports‘应为全局对象,如果以字符串形式给出,则可以在该对象上找到'relatedModel’。
如果在定义调用中需要特殊的“exports”值作为依赖项,然后在返回之前将模块放在exports对象上,则可以将该模块作为字符串或exports的成员进行引用。
在ModuleModel.js中:
define(['exports', 'use!backbone', 'use!backbone-relational'], function(exports, Backbone) {
var ModuleModel = Backbone.RelationalModel.extend({
relations: [
{
type: Backbone.HasMany,
key: 'groups',
relatedModel: 'ModuleModel',
collectionType: 'ModuleCollection'
}
]
});
exports.ModuleModel = ModuleModel;
return ModuleModel;
});在ModuleCollection.js中:
define(['exports', 'use!backbone'], function(exports, Backbone) {
var ModuleCollection = Backbone.RelationalModel.extend({
url: '/api/v1/module/';
model: exports.ModuleModel;
});
exports.ModuleCollection = ModuleCollection;
return ModuleCollection;
});https://stackoverflow.com/questions/9962695
复制相似问题