Skip to content

Commit

Permalink
Add CORS support for IE9
Browse files Browse the repository at this point in the history
- Add plugin for XDomainRequest support
- Explicity set crossDomain to true for all ajax calls
  • Loading branch information
atogle committed Jun 16, 2014
1 parent 9b20509 commit 79683c7
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 2 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"backbone": "~1.1.x",
"jquery": "2.1.0",
"leaflet-0.7.2": "http://leaflet-cdn.s3.amazonaws.com/build/leaflet-0.7.2.zip",
"gatekeeper": "https://github.com/openplans/gatekeeper.git#~0.1.x"
"gatekeeper": "https://github.com/openplans/gatekeeper.git#~0.1.x",
"jqxdomainrequest": "https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest.git#v1.0.2"
},
"devDependencies": {
"sinonjs": "~1.9.x"
Expand Down
1 change: 1 addition & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@


<script src="http://rawgit.com/openplans/gatekeeper/gh-pages/gatekeeper.js"></script>
<script src="http://rawgit.com/MoonScript/jQuery-ajaxTransport-XDomainRequest/master/jQuery.XDomainRequest.js"></script>
<script src="../src/utils.js"></script>
<script src="../src/auth.js"></script>
<script src="../src/models.js"></script>
Expand Down
8 changes: 8 additions & 0 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ var Shareabouts = Shareabouts || {};
$el = $(options.el),
map, layoutHtml, i, layerOptions, panelLayout;

// For CORS in IE9 and below, we need to POST our requests and tell
// the Shareabouts API what method to actually use in the header.
if (!$.support.cors && window.XDomainRequest) {
Backbone.emulateHTTP = true;
}

// Set any default options
options = options || {};
// _.defaults(options, {
Expand Down Expand Up @@ -202,6 +208,8 @@ var Shareabouts = Shareabouts || {};
this.placeCollection.fetchAllPages({
// So we can add the geojson in bulk, not on add
silent: true,
// Explicitly set this. IE9 forgets sometimes.
crossDomain: true,
pageSuccess: function(collection, data) {
self.geoJsonLayer.addData(data);
}
Expand Down
2 changes: 2 additions & 0 deletions src/place-form-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ var Shareabouts = Shareabouts || {};
this.model.collection = this.collection;
this.model.save(data, {
wait: true,
// Explicitly set this. IE9 forgets sometimes.
crossDomain: true,
success: function(evt) {
// Cool, now add it to the collection.
self.collection.add(self.model);
Expand Down
9 changes: 8 additions & 1 deletion src/place-support-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ var Shareabouts = Shareabouts || {};
attrs = NS.Util.getAttrs(this.ui.form);
attrs['user_token'] = self.userToken;
this.collection.create(attrs, {
// Explicitly set this. IE9 forgets sometimes.
crossDomain: true,
wait: true,
// success: function() {
// S.Util.log('USER', 'place', 'successfully-support', self.collection.options.placeModel.getLoggingDetails());
// },
error: function() {
self.getSubmitterSupport().destroy();
self.getSubmitterSupport().destroy({
// Explicitly set this. IE9 forgets sometimes.
crossDomain: true
});
alert('Oh dear. It looks like that didn\'t save.');
// S.Util.log('USER', 'place', 'fail-to-support', self.collection.options.placeModel.getLoggingDetails());
}
Expand All @@ -52,6 +57,8 @@ var Shareabouts = Shareabouts || {};
evt.target.disabled = true;

userSupport.destroy({
// Explicitly set this. IE9 forgets sometimes.
crossDomain: true,
wait: true,
// success: function() {
// S.Util.log('USER', 'place', 'successfully-unsupport', self.collection.options.placeModel.getLoggingDetails());
Expand Down
2 changes: 2 additions & 0 deletions src/place-survey-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ var Shareabouts = Shareabouts || {};
// So we know how to make the model url to save.
this.model.collection = this.collection;
this.model.save(data, {
// Explicitly set this. IE9 forgets sometimes.
crossDomain: true,
wait: true,
success: function(evt) {
// Cool, now add it to the collection.
Expand Down
8 changes: 8 additions & 0 deletions src/streetview.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ var Shareabouts = Shareabouts || {};
$el = $(options.el),
map, layoutHtml, i, layerOptions, panelLayout;

// For CORS in IE9 and below, we need to POST our requests and tell
// the Shareabouts API what method to actually use in the header.
if (!$.support.cors && window.XDomainRequest) {
Backbone.emulateHTTP = true;
}

// Set any default options
options = options || {};
// _.defaults(options, {
Expand Down Expand Up @@ -209,6 +215,8 @@ var Shareabouts = Shareabouts || {};

// Get all of the places near the center
this.placeCollection.fetchAllPages({
// Explicitly set this. IE9 forgets sometimes.
crossDomain: true,
data: {
near: mapOptions.center[0] + ',' + mapOptions.center[1],
distance_lt: mapOptions.maxDistance
Expand Down

0 comments on commit 79683c7

Please sign in to comment.