diff --git a/test/mapSpec.js b/test/mapSpec.js index 03e738e..fa203ab 100644 --- a/test/mapSpec.js +++ b/test/mapSpec.js @@ -8,7 +8,7 @@ describe('uiMap', function () { })); function createMap(options, events) { - scope.gmapOptions = options || {}; + scope.gmapOptions = (typeof(options) !== 'undefined') ? options : {}; scope.gmapEvents = events || {}; $compile("
")(scope); @@ -35,6 +35,15 @@ describe('uiMap', function () { expect(scope.gmap).toBeTruthy(); }); + it('should wait until having ui-options object before binding google map object to scope', function () { + createMap(false); + expect(scope.gmap).not.toBeTruthy(); + + scope.gmapOptions = {}; + scope.$apply(); + expect(scope.gmap).toBeTruthy(); + }); + it('should create google map with given options', function () { var center = new google.maps.LatLng(40, 40); createMap({center: center}); diff --git a/ui-map.js b/ui-map.js index 33e377d..05c092d 100644 --- a/ui-map.js +++ b/ui-map.js @@ -17,6 +17,19 @@ }); } + function whenThruthyInScope(variable, scope, callback) { + var unregisterWatch; + + if (scope.$eval(variable)) { + callback(); + } else { + unregisterWatch = scope.$watch(variable, function() { + unregisterWatch(); + whenThruthyInScope(variable, scope, callback); + }); + } + } + app.value('uiMapConfig', {}).directive('uiMap', ['uiMapConfig', '$parse', function (uiMapConfig, $parse) { @@ -30,14 +43,16 @@ restrict: 'A', //doesn't work as E for unknown reason link: function (scope, elm, attrs) { - var opts = angular.extend({}, options, scope.$eval(attrs.uiOptions)); - var map = new google.maps.Map(elm[0], opts); - var model = $parse(attrs.uiMap); + whenThruthyInScope(attrs.uiOptions, scope, function() { + var opts = angular.extend({}, options, scope.$eval(attrs.uiOptions)); + var map = new google.maps.Map(elm[0], opts); + var model = $parse(attrs.uiMap); - //Set scope variable for the map - model.assign(scope, map); + //Set scope variable for the map + model.assign(scope, map); - bindMapEvents(scope, mapEvents, map, elm); + bindMapEvents(scope, mapEvents, map, elm); + }); } }; }]);