Utah
Ortus Solutions
Prolific Module Author
1 wife, 3 kids, 1 dog
Type 1 Diabetic
npm install @inertiajs/inertia @inertiajs/inertia-vue vue
npm install --save-dev coldbox-elixir @babel/plugin-syntax-dynamic-import
// webpack.config.js
const elixir = require("coldbox-elixir");
// used for code splitting
elixir.config.mergeConfig({
optimization: {
splitChunks: {
cacheGroups: {
shared: {
chunks: "async",
minChunks: 2,
name: "includes/js/pages/shared"
}
}
}
}
});
module.exports = elixir(mix => {
mix.vue("app.js");
});
// resources/assets/js/app.js
import Vue from "vue";
import { InertiaApp } from "@inertiajs/inertia-vue";
Vue.use(InertiaApp);
let app = document.getElementById("app");
new Vue({
render: h =>
h(InertiaApp, {
props: {
initialPage: JSON.parse(app.dataset.page),
resolveComponent: name =>
import(
/* webpackChunkName: "includes/js/pages/[request]" */ `@/Pages/${name}`
).then(module => module.default)
}
})
}).$mount(app);
box install cbInertia
// handlers/Dashboard.cfc
component {
property name="inertia" inject="@cbInertia";
function index( event, rc, prc ) {
inertia.render( "Dashboard/Index" );
}
}
// resources/assets/js/Pages/Dashboard/Index.vue
<template>
<div>
<h1>Dashboard</h1>
<p>Hey there! Welcome to my Dashboard!</p>
<div>
<inertia-link href="/">Home Page</inertia-link>
</div>
</div>
</template>
<script>
export default {}
</script>
// handlers/Users.cfc
component {
property name="inertia" inject="@cbInertia";
property name="userService" inject="quickService:User";
function index( event, rc, prc ) {
inertia.render( "Users/Index", {
"users": userService.asMemento().all()
} );
}
}
// resources/assets/js/Pages/Users/Index.vue
<template>
<div>
<h1>Users</h1>
<div>
<inertia-link href="/users/new">
Create User
</inertia-link>
</div>
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" :key="user.id">
<td>
<inertia-link :href="`/users/${user.id}/edit`">
<img v-if="user.photo" :src="user.photo">
{{ user.name }}
</inertia-link>
</td>
<td>
<inertia-link :href="`/users/${user.id}/edit`" tabindex="-1">
{{ user.email }}
</inertia-link>
</td>
<td>
<inertia-link :href="`/users/${user.id}/edit`" tabindex="-1">
{{ user.owner ? 'Owner' : 'User' }}
</inertia-link>
</td>
</tr>
<tr v-if="users.length === 0">
<td class="border-t px-6 py-4" colspan="3">No users found.</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<script>
export default {
props: {
users: {
type: Array,
required: true
},
}
}
</script>
// handlers/Users.cfc
component {
property name="userService" inject="quickService:User";
function index( event, rc, prc ) {
inertia().render( "Users/Index", {
"users": userService.asMemento().all()
} );
}
}
// handlers/Users.cfc
component {
property name="userService" inject="quickService:User";
function index( event, rc, prc ) {
inertia( "Users/Index", {
"users": userService.asMemento().all()
} );
}
}
// resources/assets/js/Pages/Users/Create.vue
<template>
<div>
<h1>
<inertia-link href="/users">Users</inertia-link>
<span>/</span> Create
</h1>
<div>
<form @submit.prevent="submit">
<div>
<TextInput v-model="form.first_name" label="First name" />
<TextInput v-model="form.last_name" label="Last name" />
<TextInput v-model="form.email" label="Email" />
<TextInput v-model="form.password" type="password" label="Password" />
<SelectInput v-model="form.owner" label="Owner">
<option :value="true">Yes</option>
<option :value="false">No</option>
</SelectInput>
</div>
<div>
<LoadingButton :loading="sending" type="submit">Create User</loading-button>
</div>
</form>
</div>
</div>
</template>
<script>
import Layout from '@/Shared/Layout'
import LoadingButton from '@/Shared/LoadingButton'
import SelectInput from '@/Shared/SelectInput'
import TextInput from '@/Shared/TextInput'
export default {
components: {
LoadingButton,
SelectInput,
TextInput,
},
data() {
return {
sending: false,
form: {
first_name: null,
last_name: null,
email: null,
password: null,
owner: false,
photo: null,
},
}
},
methods: {
submit() {
this.sending = true
var data = new FormData()
data.append('first_name', this.form.first_name || '')
data.append('last_name', this.form.last_name || '')
data.append('email', this.form.email || '')
data.append('password', this.form.password || '')
data.append('owner', this.form.owner ? '1' : '0')
this.$inertia.post("/users", data, {
onFinish: () => {
this.sending = false;
}
});
},
},
}
</script>
// handlers/Users.cfc
component {
function new( event, rc, prc ) {
inertia( "Users/Create" );
}
function create( event, rc, prc ) {
var data = validateOrFail( target = rc, constraints = {
"first_name": { "required": true, "size": "1..50" },
"last_name": { "required": true, "size": "1..50" },
"email": { "required": true, "size": "1..50", "type": "email" },
"password": { "required": false },
"owner": { "required": true, "type": "boolean" }
} );
auth().user().getAccount().users().create( {
"first_name" = data.first_name,
"last_name" = data.last_name,
"email" = data.email,
"password" = event.getValue( "password", "" ),
"owner" = data.owner
} );
relocate( "users" );
}
}
// interceptors/InertiaShareInterceptor.cfc
component {
property name="inertia" inject="provider:Inertia@cbInertia";
property name="auth" inject="provider:AuthenticationService@cbauth";
function preProcess() {
inertia.share( "auth", {
"user": function() {
return auth.check() ?
auth.user().getMemento() :
javacast( "null", "" );
}
} );
inertia.share( "errors", function() {
return flash.get( "errors", {} );
} );
}
}
// resources/assets/js/Pages/Dashboard/Index.vue
<template>
<div>
<h1>Dashboard</h1>
<p>Hey there! Welcome to the Dashboard.</p>
</div>
</template>
<script>
import Layout from '@/Shared/Layout'
export default {
metaInfo: { title: 'Dashboard' },
layout: Layout,
}
</script>
// resources/assets/js/Shared/Layout.vue
<template>
<div>
<div>
<div>
<inertia-link href="/">
<Logo width="120" height="28" />
</inertia-link>
</div>
<div>
<div>{{ $page.props.auth.user.account.name }}</div>
</div>
</div>
<div>
<div scroll-region>
<slot />
</div>
</div>
</div>
</template>
<script>
import Logo from '@/Shared/Logo'
export default {
components: {
Logo
}
}
</script>
box coldbox create app skeleton=cbTemplate-quick-tailwind-inertia