[英]one to many and one to one relation with RequireJS and Backbone
我有两个带有两个模型User
和File
amd模块。 User
有许多files
, File
有一个User
。
define('model/user', ['backbone'], function (Backbone) {
return Backbone.Model.exend({
getFiles: function () {
...
}
});
});
和
define('model/file', ['backbone'], function (Backbone) {
return Backbone.Model.exend({
getUser: function () {
...
}
});
});
在控制器中已经创建的所有实体。
define('controller/users', ['backbone', 'model/user'], function (Backbone, User) {
var users = new Backbone.Collection([], {
model: User
});
users.fetch();
});
和
define('controller/files', ['backbone', 'model/file'], function (Backbone, File) {
var files = new Backbone.Collection([], {
model: File
});
files.fetch();
});
我不知道如何建立这些交叉关系(方法getFiles
和getUser
)?
我不能使model/user
需要model/file
,而这将需要model/user
。 模型不需要控制器。 也许应该在其中包含所有创建的模型的第三个模块store
中? 那他们应该怎么到达那里呢?
如果您在前端使用骨干网,则无需在模型层中定义关系,而只需在控制器层中将它们定义即可轻松摆脱困境。 将相关模型耦合在一起不一定是最好的模式。
但是,如果您希望关系成为模型层的一部分,我将研究诸如http://backbonerelational.org/之类的库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.