Search code examples
jsonviewmodeldurandal

Navigate to viewModel and pass Model value


I am getting the data from server in JSON and I am converting that to my viewModal. Now I am loading the a new view using return router.navigate('results'). But at the same time I want to pass the viewModel which I created from the JSON object to be passed to this view. By default the "results" viewModel is getting called but it is having blank values. A small code snippet will be helpful as I am new to Durandal JS.


Solution

  • Ended up finally something like this.

    1. In the module where I want to navigate, I created an "Initialize" function which accepts a JSON object. Using this JSON object I initialized all properties in the viewModel.
    2. From the page from where I have to navigate there I did a "require" on the module where I want to navigate next. Then I called the "Initialize" method and passed my JSON object.
    3. After this I used router.navigate method to go that module.

    This way when I navigated, I got all the values which I wanted to pass from one view to other. Hope this approach will help someone else.