From d0a0c4c6461343af38670fa76d2d9684da7b72e4 Mon Sep 17 00:00:00 2001 From: Ruslan Kabalin Date: Tue, 2 Jun 2015 22:56:04 +0100 Subject: [PATCH] Refactor mapBounds object in reactive variable. --- client/templates/locations/locations_page.js | 17 +++++++++++++---- server/publications.js | 16 +++++----------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/client/templates/locations/locations_page.js b/client/templates/locations/locations_page.js index 106b1f8..778d846 100644 --- a/client/templates/locations/locations_page.js +++ b/client/templates/locations/locations_page.js @@ -30,8 +30,14 @@ Template.locationsPage.onRendered(function() { if (bounds) { var ne = bounds.getNorthEast(); var sw = bounds.getSouthWest(); - var boundsObject = {ne: {lng: ne.lng(), lat: ne.lat()}, sw: {lng: sw.lng(), lat: sw.lat()}}; - instance.mapBounds.set(boundsObject); + var boundsCoordinates = [ + [ne.lng(), ne.lat()], + [sw.lng(), ne.lat()], + [sw.lng(), sw.lat()], + [ne.lng(), sw.lat()], + [ne.lng(), ne.lat()] + ]; + instance.mapBoundsCoordinates.set(boundsCoordinates); } }); c.stop(); @@ -48,11 +54,14 @@ Template.locationsPage.onCreated(function() { var instance = this; // Init reactive var. - instance.mapBounds = new ReactiveVar({}); + instance.mapBoundsCoordinates = new ReactiveVar([]); instance.autorun(function () { // Subscribe to the locations publication. - var subscription = instance.subscribe('locations', instance.mapBounds.get()); + var subscription = instance.subscribe('locations', instance.mapBoundsCoordinates.get()); + if (subscription.ready()) { + console.log(instance.locations().count()); + } }); // Locations cursor. instance.locations = function() { diff --git a/server/publications.js b/server/publications.js index 76b8e5a..b522607 100644 --- a/server/publications.js +++ b/server/publications.js @@ -1,20 +1,14 @@ Meteor.publish('locations', function(bounds) { - if (Match.test(bounds, null)) { - check(bounds, null); + if (Match.test(bounds, [Number])) { + check(bounds, [Number]); return []; } - check(bounds, {ne: {lng: Number, lat: Number}, sw: {lng: Number, lat: Number}}); + check(bounds, [[Number]]); var polygon = { type: "Polygon", - coordinates: [[ - [bounds.ne.lng, bounds.ne.lat], - [bounds.sw.lng, bounds.ne.lat], - [bounds.sw.lng, bounds.sw.lat], - [bounds.ne.lng, bounds.sw.lat], - [bounds.ne.lng, bounds.ne.lat] - ]] + coordinates: [bounds] }; - return Locations.find({location: {$geoWithin: {$geometry: polygon}}});; + return Locations.find({location: {$geoWithin: {$geometry: polygon}}}, {sort: {submitted: -1}}); }); Meteor.publish('comments', function(locationId) {