@red-code-mp/mp-builder v1.0.0
Table of contents
Structure
Each module has its own route, endpoints, menu sections and table actions
rather than grouping into one file.
Each module must have facade.js contains the files that you should register in the Base Module by Bundle Module
In this project, We are use Bootstrap-Vue Icons plugin.
// module/facade.js
export default {
Routes,
Components,
EndPoints,
ArLang,
EnLang,
Menu
}//Bundle/bundle.js
export default [
...facades
]Route
You must create new route.js file in a module, then export it from facade.js
export default [
{
path: '',
component: '',
children: []
},
]Endpoints
You must create new endpoints.js file in a module, then export it from facade.js
let base_url = 'api/model'
let endPoints = {
model :{
fetch : {
method: 'get',
url: `${base_url}`
},
find:{
method: 'get',
url: `${base_url}/:id`
},
update: (event) => {}
}
}
export default endPoints;Hints:
event, if you need to usethisvue instance
Default Endpoints
defaultEndPoints(root){
return {
fetch :{
method: 'get',
url : `api/${root}`
},
find: {
method: 'get',
url : `api/${root}/:id`
},
update: {
method: 'post',
url : `api/${root}/:id`
},
delete: {
method: 'delete',
url : `api/${root}/:id`
},
status: {
method: 'post',
url :`api/${root}/:id/is_active`
}
}
}Hints:
- If the endpoint found in
defaultEndPoints, you can skip register this endpoint. By default, ifroutemethod does not find the endpoint, it will search for it indefaultEndPoints.
Make Request
To make request, you must use rquest method.
Example:
console.log(this.route('model.find',{id: 1}))
//result >> {method: get, url: 'api/model/1'}
console.log(this.route('model.find',{id: 1}, {page: 1}))
//result >> {method: get, url: 'api/model/1?page=1'}this.request(this.route('model.find',{id: 1})).then((response)=>{})Hints:
routeandrequestmixin methods.routeaccept three arguments,the first argument receive a string that split it to array of keys
requiredthe second argument receive object that replace url params with it
optionalthe third argument receive object that appends it to url as query params
optionalrequestaccept two arguments,the first argument receive the result of
routemethod (object contains method and url after processing url)requiredthe second argument receive object (request body)
optionalYou must set colon (:) before every url param.
Table
In this project, we use jsGoodTable plugin, so you can props three objects to component
columnsrequiredendpointrequiredactionsoptionalfiltersoptional
Example #columns
let columns = [
{
label: '#',
field: 'g-no',
},
{
label: 'id',
field: 'id',
hidden: true
},
{
label: 'Name',
field: 'name',
},
{
label: 'actions',
field: 'g-actions'
},
]Hints:
- If the
fieldisn't exists in the response collection, table builder callsgetColumnComponentsthat's return array of components names, then check if field exists in it or not. g-noandg-actionsare global columns.- You can customize the column by adding name of component to
getColumnComponentsarray.
Example #endpoint
let endpoint = {
route: 'model.fetch',
params: {} // optional
}Hints:
- You can set type of params as method and its argument receive
thisvue instance.
Example #filters
let filters = [
{
type: 'FInput',
title: 'name',
slug: 'f_name',
placeholder: 'enter name ..'
},
{
type: 'FSelect',
title: 'bank',
slug: 'f_bank',
options: {
default: [
{
id: 1,
name: 'Palestine',
disabled: true
},
],
endPoint: {
route: 'banks.fetch',
params: {}, //optional
query: {name: 'PLC'} //optional
},
},
attributes: {
normalizer(node) {
return {
id: node.id,
label: node.name
}
},
placeholder: "please select bank .."
},
},
{
type: 'FDate',
title: 'created_at',
slug: 'f_created_at'
}
];Hints:
- You just have to set
slug(key of filter) and process it from backend. defaultoption is optional, that's allow you to set manual options before or after endpoint response.endpointrequired option.- We are use
Vue Treeselect, so you need tonormalizeresponse.
Example #actions
let actions = [
{
component: 'b-reference-actions',
references: [
{
slug: 'edit',
refs: (self) => {}
},
{
slug: 'delete',
refs: () => {
return {
callback: (event) => {}
}
}
},
{
slug: 'info'
}
]
},
{
component: 'bv-icon',
attributes: {
title: 'star',
icon: "question-circle",
'font-scale': "1",
variant: "primary"
},
callback: (event) => {
console.log(event)
}
},
{
component: 'bs-dropdown',
icon:{
attributes:{}
},
items:[
{
title: 'activate',
icon:{
attributes:{}
},
callback: (event) => {}
},
{
title: 'deactivate',
icon:{
attributes:{}
},
callback: (event) => {}
}
]
},
{
component: 'bv-dropdown',
title: 'more',
icon:{
attributes:{}
},
items:[
{
title: 'one',
attributes:{},
icon:{
attributes:{}
},
callback: (event) => {}
}
]
}
]Hints:
bv-icon,bs-dropdown,bv-dropdownandb-reference-actionsare global component.- You can customize the action by adding name of component to
getActionComponentsarray. callbackto do action after click icon or dropdown item.b-reference-actionscomponent for global action like edit, delete and redirect, just setslugproperty.callbackinreferencesproperty to do action after perform first callback in global action.
Example: #How to use
// table.js
const config = {
columns,
filters,
actions,
endpoint
}
export default config// list.vue
<template>
<container :header="header">
<g-table :config="config"></g-table>
</container>
</template>
<script>
import config from "./partials/table";
export default {
data(){
return{
config: config,
header: ''
}
},
}
</script>Lang
You must create new ar.js and en.js files in a module, then export it from facade.js.
We are use Vue I18n plugin for localization.
Menu
You must create new menu.js file in a module, then export it from facade.js.
Example:
export default [
{
component: 'v-menu-item',
title: 'example',
icon: 'person-lines-fill',
route: 'example.show'
},
{
component: 'v-menu-section',
title: 'General'
},
{
component: 'v-sub-menu-item',
title: 'model',
items: [
{
component: 'v-menu-item',
title: 'list',
route: 'model.list'
},
{
component: 'v-menu-item',
title: 'create',
route: 'model.create'
},
]
},
]Toast
In this project, we are use two type of alerts
You can use popDefaultToast mixin method use bootstrap-vue toast or popSwalToast method use swal to alert message.
popDefaultToastmethod receive two argument, text message and variant color(default info)popSwalToastmethod receive two argument, icon(default success) and text message.
You can use popSwalConfirm method to show confirm dialog.
popSwalConfirmmethod use swal plugin.popSwalConfirmmethod receive two argument, title and confirmation text
4 years ago