|
1 |
| -Utils |
| 1 | +# Overview |
| 2 | + |
| 3 | +The [vue-utils](https://github.com/viur-framework/vi-vue-utils) serve as an interface between the [ViUR framework](https://www.viur.dev/) and the frontend in [Vue.js](https://vuejs.org/). |
| 4 | + |
| 5 | +- [HTTP Requests](#http-requests) from ViUR backend. |
| 6 | +- [Login screen](#login-screen) for Vue.js applications with a ViUR backend. |
| 7 | +- [Auto rendering](#bone-rendering) of so called ViUR backend skeletons in a Vue.js application. |
| 8 | + |
| 9 | + |
| 10 | +## HTTP Requests |
| 11 | + |
| 12 | +```js |
| 13 | +import {Request, ListRequest, destroyStore} from '@viur/vue-utils' |
| 14 | + |
| 15 | +const simpleRequest = ref() //reactive variable |
| 16 | +Request.get("https://jsonplaceholder.typicode.com/todos/1").then(async (resp: Response)=>{ |
| 17 | + simpleRequest.value = await resp.json() //decode and set reactive variable |
| 18 | +}) |
| 19 | + |
| 20 | + |
| 21 | +let userList = ListRequest( |
| 22 | + "teststore", // unqiue name of the Pinia store |
| 23 | + {url:"/user/list", params:{"limit":10}} |
| 24 | +) |
| 25 | + |
| 26 | +//fetches all users |
| 27 | +userList.fetchAll().catch((e)=>{ |
| 28 | + console.log(e) |
| 29 | + console.log(e.statusCode) |
| 30 | + console.log(e.statusText) |
| 31 | + console.log(e.response) |
| 32 | +}) |
| 33 | + |
| 34 | +// if the results and the request is not needed anymore |
| 35 | +destroyStore(userList) |
| 36 | +``` |
| 37 | + |
| 38 | +## Login Screen |
| 39 | +coming soon... |
| 40 | + |
| 41 | +## Bone rendering |
| 42 | +```js |
| 43 | +// Global Configuration in main.js of your Vue.js application |
| 44 | +import { createApp } from "vue"; |
| 45 | + |
| 46 | +// !important use imports before importing the App.vue |
| 47 | +import bone from "@viur/vue-utils/bones/edit/bone.vue"; |
| 48 | +import Wrapper_nested from "@viur/vue-utils/bones/edit/wrapper_nested.vue"; |
| 49 | +import App from "./App.vue"; |
| 50 | + |
| 51 | +// always import as dependency for auto rendering |
| 52 | +import CKEditor from "@ckeditor/ckeditor5-vue"; |
| 53 | + |
| 54 | +const app = createApp(App); |
| 55 | + |
| 56 | +// auto rendering dependency |
| 57 | +app.use(CKEditor); |
| 58 | + |
| 59 | +// registering as components |
| 60 | +app.component("Bone", bone); |
| 61 | +app.component("Wrapper_nested", Wrapper_nested); |
| 62 | + |
| 63 | +app.mount("#app"); |
| 64 | + |
| 65 | + |
| 66 | +// Example component for auto rendering |
| 67 | +<template> |
| 68 | +<div v-for="boneName in state.structure" :key="boneName"> |
| 69 | + <bone |
| 70 | + :is="getBoneWidget(state.structure[boneName].type)" |
| 71 | + v-show="state.structure[boneName].visible" |
| 72 | + :name="boneName" |
| 73 | + :structure="state.structure" |
| 74 | + :errors="state.errors[boneName] ? state.errors[boneName] : []" |
| 75 | + :skel="state.skellist" |
| 76 | + @change="changeEvent(boneName, $event)" |
| 77 | + > |
| 78 | + </bone> |
| 79 | +</div> |
| 80 | + |
| 81 | +// implement a button for example if u want to edit data after rendering |
| 82 | +<button @click="saveData"> |
| 83 | + ... |
| 84 | +</button> |
| 85 | +</template> |
| 86 | + |
| 87 | +<script> |
| 88 | +import { onBeforeMount, reactive } from "vue"; |
| 89 | +import { Request } from "@viur/vue-utils"; |
| 90 | +import { getBoneWidget } from "@viur/vue-utils/bones/edit/index"; |
| 91 | + |
| 92 | +export default { |
| 93 | + props:{}, |
| 94 | + components:[], |
| 95 | + setup(props, ctx) { |
| 96 | + const state = reactive({ |
| 97 | + skellist: [], |
| 98 | + structure: {}, |
| 99 | + errors: {}, |
| 100 | + formData: {}, |
| 101 | + }) |
| 102 | + |
| 103 | + function changeEvent(boneName, ev) { |
| 104 | + formData[boneName] = ev.value |
| 105 | + } |
| 106 | + |
| 107 | + function saveData() { |
| 108 | + // use formData to update data on ViUR server with 'Request.securePost()'. |
| 109 | + ... |
| 110 | + } |
| 111 | + |
| 112 | + onBeforeMount() { |
| 113 | + // use Request to update state.skellist and state.structure |
| 114 | + // Note: u can use component props instead and use the Request class in parent components |
| 115 | + ... |
| 116 | + } |
| 117 | + |
| 118 | + return{ |
| 119 | + state, |
| 120 | + getBoneWidget, |
| 121 | + changeEvent, |
| 122 | + saveData, |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +</script> |
| 127 | + |
| 128 | +``` |
0 commit comments