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

4,044 views
Skip to first unread message

Vamshi Krishna Sonnathi

unread,
Mar 4, 2013, 11:17:46 AM3/4/13
to duran...@googlegroups.com
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

Joseph Gabriel

unread,
Mar 6, 2013, 10:13:41 AM3/6/13
to duran...@googlegroups.com
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?

Rainer Wittmann

unread,
Mar 6, 2013, 10:32:51 AM3/6/13
to duran...@googlegroups.com
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.


Julian Yuste

unread,
Mar 6, 2013, 10:35:25 AM3/6/13
to duran...@googlegroups.com
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.

Joseph Gabriel

unread,
Mar 6, 2013, 10:43:28 AM3/6/13
to duran...@googlegroups.com
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

Julian Yuste

unread,
Mar 6, 2013, 10:55:16 AM3/6/13
to duran...@googlegroups.com
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.

Roger Gullhaug

unread,
Mar 6, 2013, 11:25:55 AM3/6/13
to duran...@googlegroups.com
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}

Jonathan Curtis

unread,
Mar 6, 2013, 11:42:14 AM3/6/13
to Vamshi Krishna Sonnathi, duran...@googlegroups.com
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.
 
 

Rob Eisenberg

unread,
Mar 6, 2013, 4:19:41 PM3/6/13
to Jonathan Curtis, Vamshi Krishna Sonnathi, duran...@googlegroups.com
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

Gurdip Singh

unread,
Mar 11, 2013, 8:52:26 AM3/11/13
to duran...@googlegroups.com, Jonathan Curtis, Vamshi Krishna Sonnathi
Does anybody have an example of using Shared Context (3 approach from Jonathan) ? 

Baz

unread,
Jun 21, 2013, 3:18:46 PM6/21/13
to duran...@googlegroups.com, Vamshi Krishna Sonnathi
Hello,

What about child to parent?

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

Best,
Baz
Reply all
Reply to author
Forward
0 new messages