Object transfer during navigation in Durandal

I am using Durandal 2.0. I have a search and you want to pass the selected item to the detailed view. I know how to pass Id, but since the search in this case has the whole object, I would like to pass the object when navigating. I thought I could use a route with splat, but I'm not sure how to send it when I activate the route.

The route is displayed as:

router.map([ { route: '', title: 'Search', moduleId: 'viewmodels/search', nav: true }, { route: 'create', title: 'Add', moduleId: 'viewmodels/create', nav: true }, { route: 'details*movie', title: 'Details', moduleId: 'viewmodels/details', nav: false }, { route: 'edit', title: 'Edit', moduleId: 'viewmodels/edit', nav: false } ]).buildNavigationModel(); 

The search view model moves as follows:

  var openmovie = function (data) { router.navigate('details*'+ ??what do I do here??); }; 

And the detailed view model has an activation function:

  var activate = function(data) { ???what will the data be??? return true; }; 
+4
source share
1 answer

What you are trying to do is not possible using router navigation. When you move, the router internally changes the URL, so the composition life cycle starts. So basically, to do this, you will need to add the entire object to the query string of the URL, which can be pretty ugly.

Take a look at this very complete answer by Jonathan Curtis on the DurandalJS Google Groups on how to get started with this scenario.

Three approaches are proposed:

  • Parent-child
  • EventAggregator
  • General context (or model)
+4
source

Source: https://habr.com/ru/post/1500454/


All Articles