diff --git a/src/view-map.js b/src/view-map.js index 1d985a6e..948126eb 100644 --- a/src/view-map.js +++ b/src/view-map.js @@ -35,12 +35,16 @@ my.Map = Backbone.View.extend({ var self = this; this.el = $(this.el); - this.render(); this.model.bind('change', function() { self._setupGeometryField(); }); + this.model.currentDocuments.bind('add', function(doc){self.redraw('add',doc)}); + this.model.currentDocuments.bind('remove', function(doc){self.redraw('remove',doc)}); + this.model.currentDocuments.bind('reset', function(){self.redraw('reset')}); this.mapReady = false; + + this.render(); }, render: function() { @@ -65,35 +69,73 @@ my.Map = Backbone.View.extend({ return this; }, - redraw: function(){ + redraw: function(action,doc){ var self = this; + action = action || 'refresh'; + if (this.geomReady){ - if (this.model.currentDocuments.length > 0){ + if (action == 'reset'){ + // Clear all features this.features.clearLayers(); - var bounds = new L.LatLngBounds(); + } else if (action == 'add' && doc){ + // Add one or n features + this._add(doc); + } else if (action == 'remove' && doc){ + // Remove one or n features + this._remove(doc); + } else if (action == 'refresh'){ + // Clear and rebuild all features + this.features.clearLayers(); + this._add(this.model.currentDocuments.models); - this.model.currentDocuments.forEach(function(doc){ - var feature = self._getGeometryFromDocument(doc); - if (feature){ - // Build popup contents - // TODO: mustache? - html = '' - for (key in doc.attributes){ - html += '