ppoliani
ppoliani

Reputation: 4906

durandal.js viewmodel communication

Does anyone know what's the easiest way to get an instance of a view model within another view model. To be more specific, i would like to load the instance of one view model into another view model so that the latter can call methods of the former.

Does durandal keep a list of all view models that were used during the composition?

I guess one way to deal with that is applying the pub/sub pattern which is supported by durandal. Another way would probably be to manually get the view model with the aid of knockout and the dataFor utility function. However, i would like to know if there is a way to explicitly get the instance of a composed view model.

Note: my modules expose a constructor rather that singleton object

Upvotes: 2

Views: 1125

Answers (1)

Chris Shepherd
Chris Shepherd

Reputation: 1496

You can do it just by using RequireJS to bring in the viewmodel like you would another module/viewmodel. But you really want to avoid this for viewmodel communication and go with events instead.

A viewmodel will publish an event through the event aggregator and the other viewmodel is then subscribing to that event.

Check out the “Using Application-Wide Messaging” section here.

Upvotes: 2

Related Questions