2015-12-29 18 views
5

Esiste un modo per scrivere un modello di libreria utilizzando le classi es6? Posso vedere che la fonte dello scaffale stesso è stata scritta in es6. Ma tutti gli esempi e le fonti che ho trovato sono scritti in es5. Ho visto un elaborato github issue su questo che afferma che è possibile, ma in gran parte discute alcuni errori riguardanti la scrittura dei modelli nelle classi. Come scrivere e utilizzare un modello di libreria con classi es6?Scrittura di modelli di bookshelf in es6

+1

Mentre la questione è ancora da risolvere, allora ho scoperto e sono state esclusivamente con [objection.js] (https://github.com/Vincit/objection.js/) che si basa anche off di [knex] (http://knexjs.org). – Sayem

risposta

14

Sì, è possibile!

// 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);