Saturday, 28 September 2013

How to test equality of directionsService request objects

How to test equality of directionsService request objects

I'm using the Google Maps API v3 and I want to reduce the number of API
requests I'm making to a minimum.
It would make sense for me to store the last request made and test the
next being equal to the last, ignoring it if true:
request = {
origin: start,
destination: end,
waypoints: wpts,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
// Dont make a new request if same as last
if (request !== lastRequest) {
lastRequest = request;
directionsService.route(request, function(response, status) {
...
}
}
The above didn't work.
So I tried JSON.stringify:
if (JSON.stringify(request) !== JSON.stringify(lastRequest)) {
Still no dice.
Then I tried the underscore.js isEqual function:
if (!_.isEqual(request, lastRequest)) {
Again not working.
All the above return incorrect results. I'm guessing perhaps because wpts
is another object within the request object.
How should I test equality of these two objects in javascript?

No comments:

Post a Comment