Skip to content

Commit

Permalink
fix custom root view per view
Browse files Browse the repository at this point in the history
  • Loading branch information
girardinsamuel committed Sep 27, 2020
1 parent cddf3aa commit 90a9d19
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 23 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ You can find the legacy Inertia PingCRM demo with Masonite here [demo](https://g

## Features

- _Add your package main features here_
- _and here_
Almost all features of the official server-side adapters are present 😃

- Shared data
- Partial reloads
- Lazy loaded props
- Set root view in a provider
- Set root view per view
- Enable sharing Masonite routes (prefer using [masonite-js-routes](https://github.com/girardinsamuel/masonite-js-routes))
- Enable sharing Masonite flash messages

## Official Masonite Documentation

Expand Down
7 changes: 3 additions & 4 deletions app/http/controllers/InertiaController.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Inertia Demo Controller"""
from masonite.controllers import Controller
from masonite.inertia import InertiaResponse
from masonite.view import View
from masonite.request import Request

from masonite.inertia import InertiaResponse


class InertiaController(Controller):
"""Controller for testing Inertia."""
Expand All @@ -22,6 +23,4 @@ def inertia_with_error(self, view: InertiaResponse, request: Request):
return request.redirect("/")

def helloworld(self, view: InertiaResponse):
return view.render("HelloWorld", {"first_name": "Sam"}).with_root_view(
"spa_view_2"
)
return view.render("HelloWorld", {"first_name": "Sam"}, "spa_view_2")
46 changes: 45 additions & 1 deletion config/inertia.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,52 @@
| Mix Manifest
|--------------------------------------------------------------------------
|
| Absolute path to mix-manifest.json location
| Absolute path to mix-manifest.json location. It's needed for computing
| js assets version.
|
| default: root directory
"""

PUBLIC_PATH = os.getcwd()


"""
|--------------------------------------------------------------------------
| Root view
|--------------------------------------------------------------------------
|
| Root template view used by your Inertia Controllers to render the page.
| This is the name of the view without .html. This can also be overriden with
| set_root_view Inertia helper.
|
| default: app
"""

ROOT_VIEW = "spa_view"

"""
|--------------------------------------------------------------------------
| Include routes
|--------------------------------------------------------------------------
|
| Include server-side routes as JSON payload in Inertia response (props)
| Might be needed if you're not using masonite-js-routes. You can also do
| it yourself with Inertia share helper.
|
| default: False
"""

INCLUDE_ROUTES = False

"""
|--------------------------------------------------------------------------
| Share flash message
|--------------------------------------------------------------------------
|
| Include flash messages as JSON payload in Inertia response (props)
| You can also do it yourself with Inertia share helper.
|
| default: True
"""

INCLUDE_FLASH_MESSAGES = True
1 change: 0 additions & 1 deletion resources/templates/spa_view_2.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Inertia SPA</title>
<!-- {{ routes() }} -->
</head>

<body>
Expand Down
9 changes: 3 additions & 6 deletions src/masonite/inertia/core/InertiaResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ def __init__(self, container):
def set_root_view(self, root_view):
self.root_view = root_view

def with_root_view(self, root_view):
self.root_view = root_view
return self

def _load_routes(self):
from routes.web import ROUTES

Expand All @@ -43,15 +39,16 @@ def _load_routes(self):
if route.named_route:
self.routes.update({route.named_route: route.route_url})

def render(self, component, props={}):
def render(self, component, props={}, custom_root_view=None):
page_data = self.get_page_data(component, props)

if self.request.is_inertia:
self.rendered_template = json.dumps(page_data)
return self

self.rendered_template = self.view(
self.root_view, {"page": html.escape(json.dumps(page_data))}
custom_root_view if custom_root_view else self.root_view,
{"page": html.escape(json.dumps(page_data))},
).rendered_template

return self
Expand Down
16 changes: 8 additions & 8 deletions storage/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { InertiaApp } from "@inertiajs/inertia-vue";
import route from "ziggy-js";
// import { Ziggy } from "./ziggy";

Vue.mixin({
methods: {
route: (name, params, absolute) =>
route(name, params, absolute, window.Ziggy),
},
});
// Vue.mixin({
// methods: {
// route: (name, params, absolute) =>
// route(name, params, absolute, window.Ziggy),
// },
// });
// OR
// Vue.prototype.$route = (...args) => route(...args).url();

Vue.use(InertiaApp);

const app = document.getElementById("app");

// Vue.prototype.$route = (...args) => route(...args).url();

new Vue({
render: (h) =>
h(InertiaApp, {
Expand Down
1 change: 0 additions & 1 deletion storage/static/js/pages/HelloWorld.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<div>
<h1>Hello World {{ first_name }}</h1>
<inertia-link href="/">< Go back</inertia-link>
<p>Current route: {{ route().current() }}</p>
</div>
</template>

Expand Down

0 comments on commit 90a9d19

Please sign in to comment.