简体   繁体   中英

Passing an object when navigating in Durandal

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

The route is mapped 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 navigates like this:

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

And the detail view model has an activate function:

    var activate = function(data) {
    ???what will the data be???
    return true;
};

What you are trying to do is not possible using the navigation of the router. When you navigate, the router internally changes the URL so the composition lifecycle is triggered. So basically in order to make this work you would need to add all the object in the URL query string, which is something that could be pretty ugly..

Take a look at this very completed answer by Jonathan Curtis in the DurandalJS Google Groups on how to aproach this scenario.

The three approaches suggested are:

  • Parent-Child
  • EventAggregator
  • Shared Context (or model)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM