在骨干/ RequireJS嵌套列表循环依赖 [英] Circular Dependency in Backbone / RequireJS Nested List
问题描述
我设立在骨干嵌套类结构RequireJS。
I'm setting up a nested categories structure in Backbone with RequireJS.
在这种结构中,一个类别集合包含类别的模型,和一个单一的类模型可以包含一个类别集。
In this structure, a categories collection contains category models, and a single category model can contain a categories collection.
不幸的是,这似乎引起RequireJS可怕的循环依赖问题。我已阅读RequireJS的文档( http://requirejs.org/docs/api.html#circular )然而,我发现用'A'和'b'混淆。
Unfortunately this seems to cause the dreaded circular dependencies problem in RequireJS. I have read the docs on RequireJS (http://requirejs.org/docs/api.html#circular) however I am finding the explanation with 'a' and 'b' confusing.
下面是我的code,这是造成问题:
Here is my code, which is causing the problem:
define([
"jquery",
"underscore",
"backbone",
"collections/categories"
], function( $, _, Backbone, CategoriesCollection ) {
var Category = Backbone.Model.extend({
defaults: {
title: "Untitled"
},
parse: function(data) {
this.children = new CategoriesCollection( data.children, {parse: true} );
return _.omit( data, "children" );
}
});
return Category;
});
...
define([
"jquery",
"underscore",
"backbone",
"models/category"
], function( $, _, Backbone, CategoryModel ) {
var Categories = Backbone.Collection.extend({
model: CategoryModel
});
return Categories;
});
我想知道如果任何人谁经历过这可以帮助引导我在正确的方向。
I am wondering if anyone who has experienced this before can help steer me in the right direction.
感谢(提前)的帮助,
推荐答案
您只需要使用要求
集合再次当你需要它在模型中,由于集合最初的传球可以未定义:
You just need to use require
the collection again when you need it in the model, as the Collection passing initially can be undefined:
define([
"jquery",
"underscore",
"backbone",
"collections/categories"
], function( $, _, Backbone, CategoriesCollection ) {
var Category = Backbone.Model.extend({
defaults: {
title: "Untitled"
},
parse: function(data) {
if(!CategoriesCollection){
CategoriesCollection = require("collections/categories");
}
this.children = new CategoriesCollection( data.children, {parse: true} );
return _.omit( data, "children" );
}
});
return Category;
});
在他们还导入例如要求
,但它也应该没有进口。
In the example they import also require
but it should also work without the import.
这篇关于在骨干/ RequireJS嵌套列表循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!