Skip to content

Commit 639e216

Browse files
authored
Merge pull request #306 from fieg/arbitrary-ajax-settings
Use the loadEvent object to provide arbitrary $.get settings
2 parents 8308e10 + 000bddd commit 639e216

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

doc/events.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,35 @@ Triggered when a new page is about to be loaded from the server.
2020

2121
The load event object contains the following properties.
2222

23-
| property | type | description |
24-
|-----------|---------------|-------------------------|
25-
| event.url | string | url that will be loaded |
23+
| property | type | description |
24+
|-------------------|--------|--------------------------------------------------------------------------------|
25+
| event.url | string | url that will be loaded |
26+
| event.ajaxOptions | object | options that are passed to [$.ajax method](http://api.jquery.com/jquery.ajax/) |
2627

2728
Using this event it is possible to change the requested url. This can be useful to append an arbitrary parameter to the requested url so the server can handle the request differently. For example to optimize the returned html by stripping everything outside the container element (header, footer, etc.).
2829

2930
```javascript
3031
ias.on('load', function(event) {
3132
event.url = event.url + "?ajax=1";
33+
34+
// alternatively...
35+
event.ajaxOptions.data = { ajax: 1 };
36+
})
37+
```
38+
39+
The ajaxOptions property can also be used for more exotic configurations.
40+
41+
```javascript
42+
ias.on('load', function(event) {
43+
// A more exotic example, timeout and HTTP auth
44+
event.timeout = 7000; //ms
45+
event.username = 'shirley';
46+
event.password = 'temple';
3247
})
3348
```
3449

50+
See http://api.jquery.com/jquery.ajax/ for a complete list of options.
51+
3552
### loaded
3653

3754
| argument | type | description |

src/jquery-ias.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,15 @@
182182
delay = delay || this.defaultDelay;
183183

184184
var loadEvent = {
185-
url: url
185+
url: url,
186+
ajaxOptions: {
187+
dataType: 'html'
188+
}
186189
};
187190

188191
self.fire('load', [loadEvent]);
189192

190-
this.jsXhr = $.get(loadEvent.url, null, $.proxy(function(data) {
193+
function xhrDoneCallback(data) {
191194
$itemContainer = $(this.itemsContainerSelector, data).eq(0);
192195
if (0 === $itemContainer.length) {
193196
$itemContainer = $(data).filter(this.itemsContainerSelector).eq(0);
@@ -211,7 +214,10 @@
211214
callback.call(self, data, items);
212215
}
213216
}
214-
}, self), 'html');
217+
}
218+
219+
this.jsXhr = $.ajax(loadEvent.url, loadEvent.ajaxOptions)
220+
.done($.proxy(xhrDoneCallback, self));
215221

216222
return this.jsXhr;
217223
};

0 commit comments

Comments
 (0)