Here is unusual scenario I'm trying to implement:
I got gmap
module where Google Maps is asynchronously loads. I'm using following code to properly attach Google Maps:
composition.addBindingHandler('googleMap', {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
gmap = ko.utils.unwrapObservable(valueAccessor());
var latLng = new gmaps.LatLng(37.774107,-122.420853);
var mapOptions = {
center:latLng,
zoom: 17,
disableDefaultUI: true,
mapTypeId: gmaps.MapTypeId.ROADMAP
};
gmap.googleMap = new gmaps.Map(element, mapOptions);
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
});
My gmap module is module which I want to show first after the user login. So I want to perform some actions before this module should be shown. In most cases I could use a Promise in canActivate
method, but! In this case, my module doesn't attached. But I need to attach this module, perform some operations (get database records, determine user geolocation, add markers on the Google Map based on this data) and only when it's done show the module to user.
How can I get this works? I tried Promises but it didn't works for me, because I listen to attached
method which doesn't fires until the module is shown.
So the question: How to activate and attach viewmodel and it's appropriate view (activate and attached callbacks should fire) but don't show it until the condition?
UPDATE:
Okay, it seems to my question is not really clearly understandable. Here is the my scenario:
1. My SignIn module activated. User should type his credentials.
2. Once the authorization return success Gmap module should be activated and attached, but don't shown. User should still see the SignIn module and message like 'Loading data...'
3. Gmap module should be fully loaded (all the callbacks should fire). I need a possibility to interact with DOM, that means that module should be attached. But don't shown.
4. Once the some conditions is true, SignIn module comes invisible and Gmap is shown.
Promise in the activate call back doesn't fits this scenario, because view doesn't attached till the promise resolved. Basically I need something like this after the success authorization:
router.navigate('#gmap', { show: false });
...or...
router.attachModule('#gmap', ...{});
Why do I need to interact with DOM and why Promise in activate callback doesn't fits me? Because I need initiated Gmap object which can be initiate only with existing gmap canvas on the page:
composition.addBindingHandler('googleMap', {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
gmap = ko.utils.unwrapObservable(valueAccessor());
var mapOptions = {
...
};
gmap.googleMap = new gmaps.Map(element, mapOptions);
}
});
And HTML:
<div id="gmap-section">
<div id="gmap-canvas" data-bind="googleMap:map"></div>
</div>
Bindings will work only once the view attached.