Question

I am working on an ASP.NET MVC project with the durandal template.

I am facing a problem and need some help to find the more elegant solution. I have a header bar (blue) and a sidebar (grey). Both contains an element (link) to create a new transport. In the screenshot below this is showed with the bullets 1 & 2.

enter image description here

My idea was to proceed like this:

  • When clicked these 2 links navigate to #/newTransport
  • In the activate of this module I call my datacontext to create an entity with initial values and get back with the id of the transport created
  • Next, (still in this activate) I navigateTo #detailTransport/:id
  • Then the detail of this newly created transport id displayed

Here is the code:

var activate = function () {
    var id = datacontext.createTransport();
    var url = '#/detailTransport/' + id;
    router.navigateTo(url);
    return true;
};

So this newTransport view is not really showed, I used this to have a link allowing me to create the transport before showing it.

My problem is that it works only the first time and furthermore it seems to break the router logic someway.

I noticed that if I move the code from the activate to a button inside the view, I don't have problem anymore.

So for resume: I need to execute some code (create an entity) before navigating to the detail of this entity. My attempt (explained above) does not seems to work.

Any idea?

Was it helpful?

Solution

The first click should be a method in the viewmodel, not a route to activate and then redirect. Its likely confused on what you want. Navigatng in an activate is not std practice. Just make the first click bind to a method in the root vm. Then navigate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top