2015-09-12 17 views
2

我試圖遵循示例here,但是沒有運氣。思考模型在單獨的文件中:如何處理循環/循環依賴關係

我在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文件,在那裏我創建用戶和遊戲的情況下,我得到First argument of hasAndBelongsToMany must be a Model

我試圖把它寫不ES6語法,仍然無法正常工作......

回答

2

林我舉例來說,如果你將export默認改爲module.exports,一切都應該工作

1

我們需要避免循環引用所以..

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};