有什么方法可以使用 es6 类来编写书架模型吗?我可以看到书架源代码本身是用 es6 编写的。但是我遇到的所有示例和资源都是用 es5 编写的。我看到一个精心制作的github issue在此声明这是可能的,但它主要讨论了关于在类中编写模型的一些错误。如何使用 es6 类编写和使用书架模型?
最佳答案
是的,你可以!
// database.js
import config from '../../knexfile';
import knex from 'knex';
import bookshelf from 'bookshelf';
const Bookshelf = bookshelf(knex(config[process.env.NODE_ENV || 'development']));
Bookshelf.plugin('registry'); // Resolve circular dependencies with relations
Bookshelf.plugin('visibility');
export default Bookshelf;
// Administers.js
import Bookshelf from '../database'
import { createValidatorPromise as createValidator, required, email as isEmail } from '../../utils/validation';
import { User, Organization } from '../';
import { BasicAdministersView, DetailedAdministersView } from '../../views/index';
class Administers extends Bookshelf.Model {
get tableName() { return 'administers'; }
get hasTimestamps() { return true; }
view(name){
return new ({
basic: BasicAdministersView,
detailed: DetailedAdministersView
}[name])(this);
}
user() {
console.log(User);
return this.belongsTo('User', 'user_id');
}
organization() {
return this.belongsTo('Organization', 'organization_id');
}
}
export default Bookshelf.model('Administers', Administers);
关于javascript - 用es6写书架模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505139/