[英]one to many and one to one relation with RequireJS and Backbone
I have two amd modules with two models User
and File
. 我有两个带有两个模型User
和File
amd模块。 User
has many files
and File
has one User
. User
有许多files
, File
有一个User
。
define('model/user', ['backbone'], function (Backbone) {
return Backbone.Model.exend({
getFiles: function () {
...
}
});
});
and 和
define('model/file', ['backbone'], function (Backbone) {
return Backbone.Model.exend({
getUser: function () {
...
}
});
});
All entities already created in controllers. 在控制器中已经创建的所有实体。
define('controller/users', ['backbone', 'model/user'], function (Backbone, User) {
var users = new Backbone.Collection([], {
model: User
});
users.fetch();
});
and 和
define('controller/files', ['backbone', 'model/file'], function (Backbone, File) {
var files = new Backbone.Collection([], {
model: File
});
files.fetch();
});
I can't figure out how to make these cross-relationships (methods getFiles
and getUser
)? 我不知道如何建立这些交叉关系(方法getFiles
和getUser
)?
I can't make model/user
require model/file
which will require model/user
. 我不能使model/user
需要model/file
,而这将需要model/user
。 Models can't require controllers. 模型不需要控制器。 Maybe there should be third module store
with all created models in it? 也许应该在其中包含所有创建的模型的第三个模块store
中? Then how they should get there? 那他们应该怎么到达那里呢?
If you're using backbone on the front end you could easily get away with not defining the relation in the model layer, but instead - having them in the controller layer. 如果您在前端使用骨干网,则无需在模型层中定义关系,而只需在控制器层中将它们定义即可轻松摆脱困境。 Coupling related models together is not necessarily the best pattern. 将相关模型耦合在一起不一定是最好的模式。
However, if you want the relations to be a part of the model layer, I would look into libraries such as http://backbonerelational.org/ 但是,如果您希望关系成为模型层的一部分,我将研究诸如http://backbonerelational.org/之类的库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.