Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Information about current route is sent to the client #1329

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Framework/Framework/Resources/Scripts/dotvvm-base.ts
Expand Up @@ -16,6 +16,10 @@ type DotvvmCoreState = {
_viewModelCacheId?: string
_virtualDirectory: string
_initialUrl: string,
_routeName: string,
_routeParameters: {
[name: string]: any
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just thinking that it would maybe be bit better to place all these static values (culture, virtualDirectory, initialUrl, routeName, routeParameters, ...) into a common object, so we won't have to copy all the the property names on multiple places.

_stateManager: StateManager<RootViewModel>
}

Expand Down Expand Up @@ -60,7 +64,8 @@ export function clearViewModelCache() {
delete getCoreState()._viewModelCache;
}
export function getCulture(): string { return getCoreState()._culture; }

export function getRouteName(): string { return getCoreState()._routeName; }
export function getRouteParameters(): { [name: string]: any } { return getCoreState()._routeParameters; }
export function getStateManager(): StateManager<RootViewModel> { return getCoreState()._stateManager }

let initialViewModelWrapper: any;
Expand All @@ -85,7 +90,9 @@ export function initCore(culture: string): void {
_culture: culture,
_initialUrl: thisViewModel.url,
_virtualDirectory: thisViewModel.virtualDirectory!,
_stateManager: manager
_stateManager: manager,
_routeName: thisViewModel.routeName,
_routeParameters: thisViewModel.routeParameters
}

// store cached viewmodel
Expand All @@ -106,7 +113,9 @@ export function initCore(culture: string): void {
_culture: currentCoreState!._culture,
_initialUrl: a.serverResponseObject.url,
_virtualDirectory: a.serverResponseObject.virtualDirectory!,
_stateManager: currentCoreState!._stateManager
_stateManager: currentCoreState!._stateManager,
_routeName: a.serverResponseObject.routeName,
_routeParameters: a.serverResponseObject.routeParameters
}
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/Framework/Framework/Resources/Scripts/dotvvm-root.ts
@@ -1,4 +1,4 @@
import { initCore, getViewModel, getViewModelObservable, initBindings, getCulture, getState, getStateManager } from "./dotvvm-base"
import { initCore, getViewModel, getViewModelObservable, initBindings, getCulture, getState, getStateManager, getRouteName, getRouteParameters } from "./dotvvm-base"
import * as events from './events'
import * as spa from "./spa/spa"
import * as validation from './validation/validation'
Expand Down Expand Up @@ -86,6 +86,8 @@ const dotvvmExports = {
get viewModel() { return getViewModel() }
}
},
get routeName() { return getRouteName() },
get routeParameters() { return getRouteParameters() },
get state() { return getState() },
patchState(a: any) {
getStateManager().patchState(a)
Expand Down
Expand Up @@ -149,6 +149,12 @@ public void BuildViewModel(IDotvvmRequestContext context, object? commandResult)
result["renderedResources"] = JArray.FromObject(context.ResourceManager.GetNamedResourcesInOrder().Select(r => r.Name));
}

if (context.Route != null)
{
result["routeName"] = context.Route.RouteName;
result["routeParameters"] = new JObject(context.Parameters.Select(p => new JProperty(p.Key, p.Value)).ToArray());
}

// TODO: do not send on postbacks
if (validationRules?.Count > 0) result["validationRules"] = validationRules;

Expand Down