Skip to content

Commit 081fc21

Browse files
committed
Added ajaxData setting to allow custom data to be sent with AJAX request, documented setting, added additional object checks in sorting methods
1 parent 402ecaf commit 081fc21

File tree

6 files changed

+63
-35
lines changed

6 files changed

+63
-35
lines changed

dist/assets/js/plugins/storeLocator/jquery.storelocator.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
// Create the defaults once. DO NOT change these settings in this file - settings should be overridden in the plugin call
2121
var defaults = {
22+
'ajaxData' : null,
2223
'altDistanceNoResult' : false,
2324
'autoComplete' : false,
2425
'autoCompleteDisableListener': false,
@@ -584,19 +585,27 @@
584585
$('.' + this.settings.formContainer).append('<div class="' + this.settings.loadingContainer +'"></div>');
585586
}
586587

588+
// Data to pass with the AJAX request
589+
var ajaxData = {
590+
'origLat' : lat,
591+
'origLng' : lng,
592+
'origAddress': address,
593+
'formattedAddress': formattedAddress,
594+
'boundsNorthEast' : northEast,
595+
'boundsSouthWest' : southWest
596+
};
597+
598+
// Set up extra object for custom extra data to be passed with the AJAX request
599+
if (this.settings.ajaxData !== null && typeof this.settings.ajaxData === 'object') {
600+
$.extend(ajaxData, this.settings.ajaxData);
601+
}
602+
587603
// AJAX request
588604
$.ajax({
589605
type : 'GET',
590606
url : this.settings.dataLocation + (this.settings.dataType === 'jsonp' ? (this.settings.dataLocation.match(/\?/) ? '&' : '?') + 'callback=?' : ''),
591607
// Passing the lat, lng, address, formatted address and bounds with the AJAX request so they can optionally be used by back-end languages
592-
data: {
593-
'origLat' : lat,
594-
'origLng' : lng,
595-
'origAddress': address,
596-
'formattedAddress': formattedAddress,
597-
'boundsNorthEast' : northEast,
598-
'boundsSouthWest' : southWest
599-
},
608+
data : ajaxData,
600609
dataType : dataTypeRead,
601610
jsonpCallback: (this.settings.dataType === 'jsonp' ? this.settings.callbackJsonp : null)
602611
}).done(function(p) {
@@ -871,9 +880,9 @@
871880
*/
872881
sortAlpha: function(locationsarray) {
873882
this.writeDebug('sortAlpha',arguments);
874-
var property = (typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'name';
883+
var property = (this.settings.sortBy.hasOwnProperty('prop') && typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'name';
875884

876-
if (this.settings.sortBy.order === 'desc') {
885+
if (this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
877886
locationsarray.sort(function (a, b) {
878887
return b[property].toLowerCase().localeCompare(a[property].toLowerCase());
879888
});
@@ -891,9 +900,9 @@
891900
*/
892901
sortDate: function(locationsarray) {
893902
this.writeDebug('sortDate',arguments);
894-
var property = (typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'date';
903+
var property = (this.settings.sortBy.hasOwnProperty('prop') && typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'date';
895904

896-
if (this.settings.sortBy.order === 'desc') {
905+
if (this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
897906
locationsarray.sort(function (a, b) {
898907
return new Date(b[property]).getTime() - new Date(a[property]).getTime();
899908
});
@@ -911,9 +920,13 @@
911920
*/
912921
sortNumerically: function (locationsarray) {
913922
this.writeDebug('sortNumerically',arguments);
914-
var property = (typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'distance';
923+
var property = (
924+
this.settings.sortBy !== null &&
925+
this.settings.sortBy.hasOwnProperty('prop') &&
926+
typeof this.settings.sortBy.prop !== 'undefined'
927+
) ? this.settings.sortBy.prop : 'distance';
915928

916-
if (this.settings.sortBy.order === 'desc') {
929+
if (this.settings.sortBy !== null && this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
917930
locationsarray.sort(function (a, b) {
918931
return ((b[property] < a[property]) ? -1 : ((b[property] > a[property]) ? 1 : 0));
919932
});
@@ -933,9 +946,9 @@
933946
this.writeDebug('sortCustom',arguments);
934947

935948
// Alphabetically, date, or numeric
936-
if (this.settings.sortBy.method === 'alpha') {
949+
if (this.settings.sortBy.hasOwnProperty('method') && this.settings.sortBy.method.toString() === 'alpha') {
937950
this.sortAlpha(locationsarray);
938-
} else if (this.settings.sortBy.method === 'date') {
951+
} else if (this.settings.sortBy.hasOwnProperty('method') && this.settings.sortBy.method.toString() === 'date') {
939952
this.sortDate(locationsarray);
940953
} else {
941954
this.sortNumerically(locationsarray);

dist/assets/js/plugins/storeLocator/jquery.storelocator.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sort-example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ <h1 class="bh-sl-title">Using Chipotle as an Example</h1>
4444

4545
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
4646
<script src="assets/js/libs/handlebars.min.js"></script>
47-
<script src="https://maps.google.com/maps/api/js?key=AIzaSyBTYkcfVmKh16yQ3joQRWGLlfgRjj6wv8c"></script>
47+
<script src="https://maps.google.com/maps/api/js"></script>
4848
<script src="assets/js/plugins/storeLocator/jquery.storelocator.js"></script>
4949
<script>
5050
$(function() {

options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
| Property | Default | Description |
44
|---|---|---|
5+
| ajaxData | null | Allows custom data to be sent with the AJAX request. Set the setting to an object with your properties and values. |
56
| altDistanceNoResult | false | Display no results message vs. all locations when closest location is further than distanceAlert setting |
67
| autoComplete | false | Set to true to enable Google Places autocomplete. Note the slight markup differences in the example file. |
78
| autoCompleteDisableListener | false | Disable the listener that immediately triggers a search when an auto complete location option is selected. |

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ filtering.
3939

4040
Version 3 has a breaking change with the dataLocation and dataType settings switching the default from XML to JSON.
4141

42+
* Added ajaxData to allow custom data to be sent with the AJAX request. The setting accepts an object.
4243
* Added altDistanceNoResult setting to display no results message vs. all locations when closest location is further than distanceAlert setting.
4344
* Added callbackAutoGeoSuccess callback that fires after the geolocation API returns a successful result.
4445
* Added callbackFormVals callback that fires after the form values have been processed from the form.

src/js/jquery.storelocator.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
// Create the defaults once. DO NOT change these settings in this file - settings should be overridden in the plugin call
1717
var defaults = {
18+
'ajaxData' : null,
1819
'altDistanceNoResult' : false,
1920
'autoComplete' : false,
2021
'autoCompleteDisableListener': false,
@@ -580,19 +581,27 @@
580581
$('.' + this.settings.formContainer).append('<div class="' + this.settings.loadingContainer +'"></div>');
581582
}
582583

584+
// Data to pass with the AJAX request
585+
var ajaxData = {
586+
'origLat' : lat,
587+
'origLng' : lng,
588+
'origAddress': address,
589+
'formattedAddress': formattedAddress,
590+
'boundsNorthEast' : northEast,
591+
'boundsSouthWest' : southWest
592+
};
593+
594+
// Set up extra object for custom extra data to be passed with the AJAX request
595+
if (this.settings.ajaxData !== null && typeof this.settings.ajaxData === 'object') {
596+
$.extend(ajaxData, this.settings.ajaxData);
597+
}
598+
583599
// AJAX request
584600
$.ajax({
585601
type : 'GET',
586602
url : this.settings.dataLocation + (this.settings.dataType === 'jsonp' ? (this.settings.dataLocation.match(/\?/) ? '&' : '?') + 'callback=?' : ''),
587603
// Passing the lat, lng, address, formatted address and bounds with the AJAX request so they can optionally be used by back-end languages
588-
data: {
589-
'origLat' : lat,
590-
'origLng' : lng,
591-
'origAddress': address,
592-
'formattedAddress': formattedAddress,
593-
'boundsNorthEast' : northEast,
594-
'boundsSouthWest' : southWest
595-
},
604+
data : ajaxData,
596605
dataType : dataTypeRead,
597606
jsonpCallback: (this.settings.dataType === 'jsonp' ? this.settings.callbackJsonp : null)
598607
}).done(function(p) {
@@ -867,9 +876,9 @@
867876
*/
868877
sortAlpha: function(locationsarray) {
869878
this.writeDebug('sortAlpha',arguments);
870-
var property = (typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'name';
879+
var property = (this.settings.sortBy.hasOwnProperty('prop') && typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'name';
871880

872-
if (this.settings.sortBy.order === 'desc') {
881+
if (this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
873882
locationsarray.sort(function (a, b) {
874883
return b[property].toLowerCase().localeCompare(a[property].toLowerCase());
875884
});
@@ -887,9 +896,9 @@
887896
*/
888897
sortDate: function(locationsarray) {
889898
this.writeDebug('sortDate',arguments);
890-
var property = (typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'date';
899+
var property = (this.settings.sortBy.hasOwnProperty('prop') && typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'date';
891900

892-
if (this.settings.sortBy.order === 'desc') {
901+
if (this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
893902
locationsarray.sort(function (a, b) {
894903
return new Date(b[property]).getTime() - new Date(a[property]).getTime();
895904
});
@@ -907,9 +916,13 @@
907916
*/
908917
sortNumerically: function (locationsarray) {
909918
this.writeDebug('sortNumerically',arguments);
910-
var property = (typeof this.settings.sortBy.prop !== 'undefined') ? this.settings.sortBy.prop : 'distance';
919+
var property = (
920+
this.settings.sortBy !== null &&
921+
this.settings.sortBy.hasOwnProperty('prop') &&
922+
typeof this.settings.sortBy.prop !== 'undefined'
923+
) ? this.settings.sortBy.prop : 'distance';
911924

912-
if (this.settings.sortBy.order === 'desc') {
925+
if (this.settings.sortBy !== null && this.settings.sortBy.hasOwnProperty('order') && this.settings.sortBy.order.toString() === 'desc') {
913926
locationsarray.sort(function (a, b) {
914927
return ((b[property] < a[property]) ? -1 : ((b[property] > a[property]) ? 1 : 0));
915928
});
@@ -929,9 +942,9 @@
929942
this.writeDebug('sortCustom',arguments);
930943

931944
// Alphabetically, date, or numeric
932-
if (this.settings.sortBy.method === 'alpha') {
945+
if (this.settings.sortBy.hasOwnProperty('method') && this.settings.sortBy.method.toString() === 'alpha') {
933946
this.sortAlpha(locationsarray);
934-
} else if (this.settings.sortBy.method === 'date') {
947+
} else if (this.settings.sortBy.hasOwnProperty('method') && this.settings.sortBy.method.toString() === 'date') {
935948
this.sortDate(locationsarray);
936949
} else {
937950
this.sortNumerically(locationsarray);

0 commit comments

Comments
 (0)