EDIT **
The answer below was for Durandal 1.2. In durandal 2.0, the viewAttached event was renamed to attached . You can read the Durandals documentation about this here .
Durandal has a viewAttached event that fires on your view model after the view has been bound and bound to dom. This would be a good place to call google maps.
define(['services/logger'], function (logger) { var vm = { viewAttached: initialize title: 'Home View', map: map }; return vm; var map; function initialize(view) { logger.log('Home View Activated', null, 'home', true); var mapOptions = { zoom: 8, center: new google.maps.LatLng(-34.397, 150.644), mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); } });
EDIT AGAIN to respond to peoples comments **
According to Jamie Hammond's comment, it is best to use your DOM transversally to the view in question. If the DOM element is separate from the view.
So, inside your viewAttached (in durandal 1.2) or attached (in durandal 2.0) you should:
var map; var mapOptions = { }; var vm = { attached: function (view) { var mapCanvas = $('#map-canvas', view).get(0); map = new google.maps.Map(mapCanvas, mapOptions); } }
I didn't bother with Durandal 2.0 at all, because I was very busy with work and things, and when I fiddled with Durandal 1.0, it was just for fun, but I really love the framework and hope to play with 2.0 someday. With that said, I had a problem creating a map in viewattached in Durandal 1.0. But I did not use Google maps. I used Leafletjs. My solution to the problem was to create a delay in the Attached view, which would redraw the map after a short delay. This was due to the fact that the transition of Durandal from the point of view did not work very well with the ability to flip a map in the dom element, since it was flying and disappearing.
So, inside viewAttached, I would draw a map like this:
window.setTimeout(drawMap, 10);
Again, this was a very specific problem that I encountered, and not a problem with Durandal. This was rather a problem, as Leafletjs did not display the map correctly when the DOM element was still transitioning.