0

Is it possible to pass observable and observable arrays from the viewmodel as arguments to other pages or as json ?

Let's say i have a couple of fields and when i push a button it will redirect to the other page and display the parameters.

Can this be accomplished with knockout/jquery or do i need to post to controller and redirect from there ?

The idea is to pass information to an confirmation page and from there proceed a submit to controller.

Thanks on forehand!

Henrik
  • 1,797
  • 4
  • 22
  • 46

2 Answers2

1

I would go as far as to say you shouldn't do either of those things. It smells like bad design.

I answered the same question (or at least very much alike) a short while ago, and I think the answer applies here as well: durandal : best way to pass data between ViewModels

Edit: I read over what your goal was. In that case I would approach it differently: never leave the page to begin with. Just create the confirmation 'page' inline in your current view, and set a flag to determine whether the form or the confirmation should be shown. This will save a lot of overhead and will be more responsive on the client side as well.

Edit 2: Fiddle you requested: http://jsfiddle.net/7zp5K/33/

Basically the trick is all in the submit function on your viewmodel where I swap the observable showConfirm. In your HTML I created 2 'views' (divs). Only one is shown based on the state of showConfirm.

In the confirmation view I use a foreach binding to show the selected items.

Community
  • 1
  • 1
Hans Roerdinkholder
  • 3,000
  • 1
  • 20
  • 30
  • My problem is that i will have like about 20 different id's, so it will be hard to route to link/id or something like that. I need to pass objects. :( The best approach in this case maybe is posting to controller and mapping to objects? – Henrik Feb 18 '14 at 14:25
  • Passing objects to another page is not a good idea at all, if you ask me. Go for the 'viewmodel-driven approach' I proposed in the topic I linked. Also, I edited my post to suggest something else entirely. – Hans Roerdinkholder Feb 18 '14 at 14:28
  • When you use Knockout, you should be aware of the subtle difference between a 'page' and a 'view'. A view is just a way of presenting data. In your case, the data stays the same all the time, but the way you present it is different (form versus read-only with confirmation check). You just need to switch the view (HTML), not the data. There is absolutely no need to make it complicated by creating a second page. – Hans Roerdinkholder Feb 18 '14 at 14:30
  • Yes, this sounds much better. The only thing i want to switch is Another view and present the details that will be submitted if you press submit button. I'm new to knockout and JavaScript so im aware of how to create an inline page within my current view. May you look at this fiddle and tell me where to put the confirmation page please? I want to display all items in both lists on the confirmation page. http://jsfiddle.net/7zp5K/31/ Thank you! – Henrik Feb 18 '14 at 14:41
  • I updated my answer with a working fiddle – Hans Roerdinkholder Feb 18 '14 at 14:57
  • Thanks a lot! Much appriciated! :) – Henrik Feb 18 '14 at 15:12
0

If I understand your question properly, you are trying to pass ViewModel from one page to another. If that is the case, then it is not possible straight away. You must create ViewModel in the redirected page based on the received data, either querystring, JSON or any other data format.

If the data is small you can pass it through querystring and create a viewmodel accordingly in the redirected page. Otherwise, use form post to submit the data. It is more of requirement specific, rather than knockout specific.

NaveenBhat
  • 3,248
  • 4
  • 35
  • 48
  • In the other page (which i will redirect to) i will take parameters in the viewmodel constructor. I will have 2 observable and 2 observable arrays which will contain 1-20 strings. So its not that large data that will be passed. If its possible i want to avoid to submit this, because i will just pass this to a confirmation page and from there submit a form. – Henrik Feb 18 '14 at 14:16