|
| 1 | +import Context from './common/context'; |
| 2 | +import Action from './actions/Action' |
| 3 | +import Fetch from './actions/Fetch' |
| 4 | +import Get from './actions/Get' |
| 5 | +import Create from './actions/Create' |
| 6 | +import Update from './actions/Update' |
| 7 | +import Delete from './actions/Delete' |
| 8 | + |
1 | 9 | export default class VuexOrmAxios { |
2 | 10 | /** |
3 | 11 | * @constructor |
4 | 12 | * @param {Components} components The Vuex-ORM Components collection |
5 | 13 | * @param {Options} options The options passed to VuexORM.install |
6 | 14 | */ |
7 | 15 | constructor(components, options) { |
8 | | - // |
| 16 | + Context.setup(components, options); |
| 17 | + this.setupActions(); |
| 18 | + this.setupModels(); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * This method will setup following Vuex actions: $fetch, $get, $create, $update, $delete |
| 23 | + */ |
| 24 | + setupActions () { |
| 25 | + const context = Context.getInstance(); |
| 26 | + |
| 27 | + context.components.Actions.$fetch = Fetch.call.bind(Fetch); |
| 28 | + context.components.Actions.$get = Get.call.bind(Get); |
| 29 | + context.components.Actions.$create = Create.call.bind(Create); |
| 30 | + context.components.Actions.$update = Update.call.bind(Update); |
| 31 | + context.components.Actions.$delete = Delete.call.bind(Delete); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * This method will setup following model methods: Model.$fetch, Model.$get, Model.$create, |
| 36 | + * Model.$update, Model.$delete |
| 37 | + */ |
| 38 | + setupModels () { |
| 39 | + const context = Context.getInstance(); |
| 40 | + |
| 41 | + /** |
| 42 | + * Transform Model and Modules |
| 43 | + */ |
| 44 | + _.map(context.database.entities, entity => { |
| 45 | + entity.module = Action.transformModule(entity.module); |
| 46 | + entity.model = Action.transformModel(entity.model); |
| 47 | + return entity; |
| 48 | + }); |
| 49 | + |
| 50 | + context.components.Model.$fetch = async function (config = {}) { |
| 51 | + return this.dispatch('$fetch', config); |
| 52 | + }; |
| 53 | + |
| 54 | + context.components.Model.$get = async function (config = {}) { |
| 55 | + return this.dispatch('$get', config); |
| 56 | + }; |
| 57 | + |
| 58 | + context.components.Model.$create = async function (config = {}) { |
| 59 | + return this.dispatch('$create', config); |
| 60 | + }; |
| 61 | + |
| 62 | + context.components.Model.$update = async function (config = {}) { |
| 63 | + return this.dispatch('$update', config); |
| 64 | + }; |
| 65 | + |
| 66 | + context.components.Model.$delete = async function (config = {}) { |
| 67 | + return this.dispatch('$delete', config); |
| 68 | + }; |
9 | 69 | } |
10 | 70 | } |
0 commit comments