How do we share the data between views/Pass the data view to view

Showing 1-11 of 11 messages
How do we share the data between views/Pass the data view to view Vamshi Krishna Sonnathi 3/4/13 8:17 AM
How Do we share the data between the views?

Use case: We will show the users list in one view, when we select user we need to show the user information in new view (labels or editable).

Any suggestions/examples

Thanks
Re: How do we share the data between views/Pass the data view to view Joseph Gabriel 3/6/13 7:13 AM
Not knowing anything about your scenario, one thing that jumps to mind is to create a requireJS module to hold/manage this information.  Then you can Require it from wherever it's needed.  You'd need to return an object, not a function, to ensure that everyone gets the same instance of the module, but keep in mind this essentially becomes a "global" variable.  

Your data example sounds like it may be better suited to something else, though.  What about passing the id of the selected user as a parameter in the route?
Re: How do we share the data between views/Pass the data view to view Rainer Wittmann 3/6/13 7:32 AM
Sounds like  a use case for event.
http://durandaljs.com/documentation/Events.html

You could pass the user object to a channel and listen in your new view.


Re: How do we share the data between views/Pass the data view to view Julian Yuste 3/6/13 7:35 AM
Hello,

I can think in two ways of do it.

Option one:
You can use Events ( http://durandaljs.com/documentation/Events.html ): At the details view  you register a callback to an event and at the list view, when a user is selected, you trigger an event containing the user information.

Option two:
I never tried to do this, but i think that i readed here at the list that you can compose a view inside another view programatically using the compose method.
Re: How do we share the data between views/Pass the data view to view Joseph Gabriel 3/6/13 7:43 AM
I'm assuming view/view model pairs, but wouldn't events require that the new view model be loaded and listening?  Not sure how that would work if it hasn't yet been navigated to
Re: How do we share the data between views/Pass the data view to view Julian Yuste 3/6/13 7:55 AM
You are right, the event alternative isn't viable if the viewmodel is not loaded.

Then, I think that the way to do it is my second option, compose the child view programatically when a user is selected.
Re: How do we share the data between views/Pass the data view to view Roger Gullhaug 3/6/13 8:25 AM
Can you use routes for this? Have one route for the user list like this: http://localhost/durandal/#/users and a seperate route for the detail view http://localhost/durandal/#/userdetails/{userno}
Re: How do we share the data between views/Pass the data view to view Jonathan Curtis 3/6/13 8:42 AM
There are typically 3 approaches to sharing data between view models:
  1. Parent-Child: Have the parent view model directly pass the data to the child. It needs a reference to the child, you can do this by requiring it as a singleton or requiring it as a constructor and creating an instance, passing in the data it needs. Typically here you would have a property on the parent that is the active child view model and compose this in the view. This approach creates coupling, but is OK in logically coupled scenarios.
  2. EventAggregator: Pass the data through a mediator. Now we are only coupled to an event contract. The child must exist and be subscribed to the aggregator (ie, been loaded already).
  3. Shared Context (or model): Have both view models rely on a shared context (the model in MVVM). This could be a module that they both require. If the data changes faster than the lifecycle of the view model, you can combine this with EventAggregator to notify the view model of changes.This is a good approach to remove coupling between view models.


--
You received this message because you are subscribed to the Google Groups "DurandalJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to durandaljs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

RE: How do we share the data between views/Pass the data view to view EisenbergEffect 3/6/13 1:19 PM
That's a pretty good summary I would say :D

Sent from my Windows Phone

-----Original Message-----
From: Jonathan Curtis
Sent: 3/6/2013 11:42 AM
To: Vamshi Krishna Sonnathi
Cc: duran...@googlegroups.com
Subject: Re: How do we share the data between views/Pass the data view to view
Re: How do we share the data between views/Pass the data view to view Gurdip Singh 3/11/13 5:52 AM
Does anybody have an example of using Shared Context (3 approach from Jonathan) ? 
Re: How do we share the data between views/Pass the data view to view Baz 6/21/13 12:18 PM
Hello,

What about child to parent?

define(['require'], function(require) {
   console.log(require('viewmodels/parentModel'));
});

Best,
Baz