在单独的文件中考虑模型:如何处理循环/循环依赖关系
thinky models in separate files: how to handle cyclical/circular dependencies
我试着遵循这个例子,但是没有成功。
我有用户模型在User .js文件:
import thinky from './thinky';
let type = thinky.type;
let User = thinky.createModel('User', {
id: type.string(),
username: type.string(),
});
export default User;
let Game = require('./game');
User.hasAndBelongsToMany(Game, "games", "id", "id");
Game .js文件中的游戏模型:
import thinky from './thinky';
let type = thinky.type;
let Game = thinky.createModel('Game', {
id: type.string(),
host: type.string()
});
export default Game;
let User = require('./user');
Game.hasAndBelongsToMany(User, "players", "id", "id");
当我尝试将它们导入到test.js文件中,在那里我创建了User和Game的实例,我得到First argument of hasAndBelongsToMany must be a Model
我试着写它没有es6语法,仍然不工作…
我的例子,如果你改变导出默认为模块。导出,一切都应该正常
我们需要避免循环引用,所以…
user.js
import thinky from './thinky';
let type = thinky.type;
let User = thinky.createModel('User', {
id: type.string(),
username: type.string(),
});
export default User;
game.js
import thinky from './thinky';
let type = thinky.type;
let Game = thinky.createModel('Game', {
id: type.string(),
host: type.string()
});
export default Game;
index.js
import User from './user';
import Game from './game';
Game.hasAndBelongsToMany(User, "players", "id", "id");
User.hasAndBelongsToMany(Game, "games", "id", "id");
export {User, Game};
你也可以尝试这个加载器,它是为加载多个模型定义而设计的,并使它们在你的应用程序中可用。
https://github.com/mwielbut/thinky-loader相关文章:
- 如何使用jquery处理php循环通过元素
- Javascript在for循环中等待处理请求
- 在循环中附加事件处理程序时出现浏览器性能问题
- 如何处理javascript中的循环引用,类似于Excel提供迭代限制的方式
- Emberjs#每个循环不处理数组数据
- jQuery:如何在没有for循环的情况下将事件处理程序应用于$('#text'+'任意整数
- 在嵌套循环中处理 AJAX (Angularjs)
- RxJS:在循环中处理错误.js自定义驱动程序
- 在处理元素单击事件期间,在循环内部调用window.open()
- 将事件处理程序分配给for循环中的每个按钮
- 如何在循环中声明事件处理程序
- 数组循环 - 处理下拉列表中的空值
- 节点.js:如何循环处理传入数据
- JavaScript 中的事件处理程序如何由事件循环处理
- Javascript异步循环处理
- 蓝鸟:链.然后循环处理
- 我可以在JavaScript中同时循环处理两个对象吗
- Jquery美元.每个循环处理数组
- 第一次使用AJAX时,JSON通过AJAX循环处理多个页面上的数据
- 如何在JavaScript中暂停主循环处理,直到按下一个键