Skip to content

Commit

Permalink
Refactor mapBounds object in reactive variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Kabalin authored and kabalin committed Feb 3, 2019
1 parent 75aabc9 commit d0a0c4c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
17 changes: 13 additions & 4 deletions client/templates/locations/locations_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand Down
16 changes: 5 additions & 11 deletions server/publications.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit d0a0c4c

Please sign in to comment.