Skip to content

Commit

Permalink
Don't restore the viewmodel on reload
Browse files Browse the repository at this point in the history
Also added a check if the history API is avaiable
  • Loading branch information
GerardSmit committed Apr 26, 2019
1 parent ccdbfb3 commit f97b2e1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 42 deletions.
73 changes: 44 additions & 29 deletions src/DotVVM.Framework/Resources/Scripts/DotVVM.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/DotVVM.Framework/Resources/Scripts/DotVVM.min.js

Large diffs are not rendered by default.

51 changes: 39 additions & 12 deletions src/DotVVM.Framework/Resources/Scripts/DotVVM.ts
Expand Up @@ -220,11 +220,30 @@ class DotVVM {
public useHistoryApiSpaNavigation: boolean;
public isPostbackRunning = ko.observable(false);

private isBrowserReload(): boolean {
if (performance) {
if (performance.getEntriesByType) {
var entries = performance.getEntriesByType("navigation");

if (entries.length > 0) {
return (<PerformanceNavigationTiming>entries[0]).type === "reload";
}
}

// deprecated in Navigation Timing Level 2 specification
if (performance.navigation) {
return performance.navigation.type === 1;
}
}

return false;
}

public init(viewModelName: string, culture: string, thisViewModel?: IDotvvmViewModelInfo): void {
this.addKnockoutBindingHandlers();

// restore the viewmodel from the history API.
if (history.state && history.state.dotvvm_viewmodels && history.state.dotvvm_viewmodels[viewModelName]) {
if (!this.isBrowserReload() && history && history.state && history.state.dotvvm_viewmodels && history.state.dotvvm_viewmodels[viewModelName]) {
// create a new object, otherwise the object in the history state will be changed which will result in serialize errors.
thisViewModel = <IDotvvmViewModelInfo>{ ...history.state.dotvvm_viewmodels[viewModelName] };
}
Expand Down Expand Up @@ -361,20 +380,28 @@ class DotVVM {
}

private persistViewModel(viewModelName: string) {
var currentState = history.state ? history.state : {};
var persistedViewModels = currentState.dotvvm_viewmodels ? currentState.dotvvm_viewmodels : {};
var thisViewModel = ko.toJS(this.viewModels[viewModelName]);

// add the new viewmodel to the existing state, otherwise SPA mode will break.
var state = {
dotvvm_viewmodels: {
[viewModelName]: thisViewModel,
...persistedViewModels
},
...currentState
};
if (history) {
var currentState = history.state ? history.state : {};
var persistedViewModels = currentState.dotvvm_viewmodels ? currentState.dotvvm_viewmodels : {};

history.replaceState(state, document.title);
// add the new viewmodel to the existing state, otherwise SPA mode will break.
var state = {
dotvvm_viewmodels: {
[viewModelName]: thisViewModel,
...persistedViewModels
},
...currentState
};

history.replaceState(state, document.title);
}

var viewModelInput = <HTMLInputElement>document.getElementById("__dot_viewmodel_" + viewModelName);
if (viewModelInput) {
viewModelInput.value = JSON.stringify(thisViewModel);
}
}

private backUpPostBackConter(): number {
Expand Down

0 comments on commit f97b2e1

Please sign in to comment.