Skip to content

Commit ab399ea

Browse files
committed
Fix link navigation reloading & causing trouble
fixes #4 Hopefully impacts matestack/matestack-docs#34
1 parent a265581 commit ab399ea

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Vue from 'vue/dist/vue.esm'
22
import VRuntimeTemplate from "v-runtime-template"
33
import Vuex from 'vuex'
4+
import isNavigatingToAnotherPage from "app/location"
45

56
const componentDef = {
67
props: ['appConfig', 'params'],
@@ -11,9 +12,10 @@ const componentDef = {
1112
asyncTemplate: state => state.pageTemplate,
1213
}),
1314
mounted: function(){
14-
const self = this;
15-
window.onpopstate = function(event) {
16-
self.$store.dispatch("navigateTo", {url: document.location.pathname, backwards: true} );
15+
window.onpopstate = (event) => {
16+
if (isNavigatingToAnotherPage(window.location, event)) {
17+
this.$store.dispatch("navigateTo", {url: document.location.pathname, backwards: true} );
18+
};
1719
}
1820
},
1921
components: {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const isNavigatingToAnotherPage = function(currentLocation, popstateEvent) {
2+
const targetLocation = popstateEvent.target.location;
3+
4+
// omits hash by design
5+
return currentLocation.pathname !== targetLocation.pathname ||
6+
currentLocation.origin !== targetLocation.origin ||
7+
currentLocation.search !== targetLocation.search
8+
}
9+
10+
export default isNavigatingToAnotherPage

spec/usage/components/link_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,11 @@ def set_app_class
262262
element = page.find("#my-div")
263263
before_content = element.text
264264

265-
puts before_content
266-
267265
# don't you rerender on me!
268266
expect(ExamplePage).not_to receive(:new)
269267

270268
page.click_link("my-link")
271269

272-
# binding.pry
273-
274270
# if the page reloaded we'd have different content here but as we don't want reloads
275271
# we want the sime
276272
expect(page).to have_css("#my-div", text: before_content)

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

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

vendor/assets/javascripts/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.

0 commit comments

Comments
 (0)