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

已查看 4,045 次
跳至第一个未读帖子

Vamshi Krishna Sonnathi

未读,
2013年3月4日 11:17:462013/3/4
收件人 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

未读,
2013年3月6日 10:13:412013/3/6
收件人 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

未读,
2013年3月6日 10:32:512013/3/6
收件人 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

未读,
2013年3月6日 10:35:252013/3/6
收件人 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

未读,
2013年3月6日 10:43:282013/3/6
收件人 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

未读,
2013年3月6日 10:55:162013/3/6
收件人 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

未读,
2013年3月6日 11:25:552013/3/6
收件人 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

未读,
2013年3月6日 11:42:142013/3/6
收件人 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

未读,
2013年3月6日 16:19:412013/3/6
收件人 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

未读,
2013年3月11日 08:52:262013/3/11
收件人 duran...@googlegroups.com、Jonathan Curtis、Vamshi Krishna Sonnathi
Does anybody have an example of using Shared Context (3 approach from Jonathan) ? 

Baz

未读,
2013年6月21日 15:18:462013/6/21
收件人 duran...@googlegroups.com、Vamshi Krishna Sonnathi
Hello,

What about child to parent?

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

Best,
Baz
回复全部
回复作者
转发
0 个新帖子