Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Waiting to initialize map until ui-options has reasonable value in scope #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion test/mapSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('uiMap', function () {
}));

function createMap(options, events) {
scope.gmapOptions = options || {};
scope.gmapOptions = (typeof(options) !== 'undefined') ? options : {};
scope.gmapEvents = events || {};
$compile("<div><div ui-map='gmap' ui-options='gmapOptions'" +
"' ui-event='gmapEvents'></div></div>")(scope);
Expand All @@ -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});
Expand Down
27 changes: 21 additions & 6 deletions ui-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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);
});
}
};
}]);
Expand Down