Skip to content

Commit ff6ec50

Browse files
committed
added turbolinks support
1 parent e37f87d commit ff6ec50

File tree

6 files changed

+135
-35
lines changed

6 files changed

+135
-35
lines changed

app/concepts/matestack/ui/core/app/store.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const store = new Vuex.Store({
2020
state.currentPathName = current.path
2121
state.currentSearch = current.search
2222
state.currentOrigin = current.origin
23+
},
24+
resetPageTemplate (state) {
25+
state.pageTemplate = null;
2326
}
2427
},
2528
actions: {
Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue/dist/vue.esm'
2+
import { turbolinksAdapterMixin } from 'vue-turbolinks';
23

34
// Import from app/concepts/matestack/ui/core:
45
import app from '../app/app'
@@ -18,23 +19,35 @@ import collectionOrder from '../collection/order/order'
1819

1920
let matestackUiApp = undefined
2021

21-
let turbolinksLoaded = false
22-
23-
document.addEventListener('turbolinks:load', () => {
24-
turbolinksLoaded = true
25-
matestackUiApp = new Vue({
26-
el: "#matestack_ui",
27-
store: store
28-
})
22+
// this event fires first and always
23+
document.addEventListener('DOMContentLoaded', () => {
24+
// somehow we need to inject the turbolinks mixin even
25+
// if the turbolinks:load event will recreate the vue instance
26+
// skipping the injection here caused errors when submitting forms or action
27+
// if they were present on the first page, which was loaded and activated turbolinks
28+
// the mixin does not impact the app when turbolinks is disabled
29+
matestackUiApp = new Vue({
30+
el: "#matestack_ui",
31+
mixins: [turbolinksAdapterMixin],
32+
store: store
33+
})
2934
})
3035

31-
document.addEventListener('DOMContentLoaded', () => {
32-
if(turbolinksLoaded === false){
33-
matestackUiApp = new Vue({
34-
el: "#matestack_ui",
35-
store: store
36-
})
37-
}
36+
// this event fires after DOMContentLoaded and only if turbolinks are enabled
37+
document.addEventListener('turbolinks:load', () => {
38+
// we need to empty the currently stored pageTemplate state variable
39+
// otherwise the matestack page will jump back to the latest pageTemplate
40+
// fetched during the last matestack transition as the turbolinks powered
41+
// page transition does not write the matestack store pageTemplate state variable
42+
store.commit('resetPageTemplate')
43+
// we need to destroy the vue app instance
44+
matestackUiApp.$destroy();
45+
// and recreate it right afterwards in order to work when used with turbolinks
46+
matestackUiApp = new Vue({
47+
el: "#matestack_ui",
48+
mixins: [turbolinksAdapterMixin],
49+
store: store
50+
})
3851
})
3952

4053
export default Vue

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"link-module-alias": "^1.2.0",
99
"v-runtime-template": "^1.5.2",
1010
"vue": "^2.5.17",
11+
"vue-turbolinks": "^2.1.0",
1112
"vuex": "^3.0.1",
1213
"yarn": "^1.22.0"
1314
},
1415
"exports": {
1516
"./concepts/": "./app/concepts/matestack/ui/core/"
16-
},
17+
},
1718
"scripts": {
1819
"postinstall": "link-module-alias"
1920
},

vendor/assets/javascripts/dist/matestack-ui-core.js

Lines changed: 89 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/assets/javascripts/dist/matestack-ui-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ v-runtime-template@^1.5.2:
9191
resolved "https://registry.yarnpkg.com/v-runtime-template/-/v-runtime-template-1.10.0.tgz#8ea7066c37cf4be5c701a06ca247e1afda89c4be"
9292
integrity sha512-WLlq9jUepSfUrMEenw3mn7FDXX6hhbl11JjC1OKhwLzifHzVrY5a696TUHDPyj9jke3GGnR7b+2T3od/RL5cww==
9393

94+
vue-turbolinks@^2.1.0:
95+
version "2.1.0"
96+
resolved "https://registry.yarnpkg.com/vue-turbolinks/-/vue-turbolinks-2.1.0.tgz#fc7372d06d624aa3fb68231dc0b509719edc778f"
97+
integrity sha512-Rgq9eUS2LgHVoSR58S0utW7g/aqLlVZ6uM7S1e02+xEUGKW1fqhUHQXea3p+cE59ldf+f3nz3Cc3HVO4MlrTFA==
98+
dependencies:
99+
vue "^2.2.4"
100+
101+
vue@^2.2.4:
102+
version "2.6.11"
103+
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.11.tgz#76594d877d4b12234406e84e35275c6d514125c5"
104+
integrity sha512-VfPwgcGABbGAue9+sfrD4PuwFar7gPb1yl1UK1MwXoQPAw0BKSqWfoYCT/ThFrdEVWoI51dBuyCoiNU9bZDZxQ==
105+
94106
vue@^2.5.17:
95107
version "2.6.10"
96108
resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.10.tgz#a72b1a42a4d82a721ea438d1b6bf55e66195c637"

0 commit comments

Comments
 (0)