有没有办法用es6类编写书架模型?我可以看到书架源本身是用es6编写的.但是我遇到的所有示例和来源都是用es5编写的.我在这里看到了一个精心设计的github问题,该问题表明它是可能的,但它主要讨论了关于在类中编写模型的几个错误.如何编写和使用带有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);