Skip to content

Commit c2a7265

Browse files
committed
update pjax... again!
1 parent 6ea1d31 commit c2a7265

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

jquery.pjax.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// jquery.pjax.js
22
// copyright chris wanstrath
3-
// https://github.com/defunkt/pjax
3+
// https://github.com/defunkt/jquery-pjax
44

55
(function($){
66

@@ -29,6 +29,13 @@ $.fn.pjax = function( container, options ) {
2929
else
3030
options = $.isPlainObject(container) ? container : {container:container}
3131

32+
// We can't persist $objects using the history API so we must use
33+
// a String selector. Bail if we got anything else.
34+
if ( typeof options.container !== 'string' ) {
35+
throw "pjax container must be a string selector!"
36+
return false
37+
}
38+
3239
return this.live('click', function(event){
3340
// Middle click, cmd click, and ctrl click should open
3441
// links in a new tab as normal.
@@ -37,7 +44,8 @@ $.fn.pjax = function( container, options ) {
3744

3845
var defaults = {
3946
url: this.href,
40-
container: $(this).attr('data-pjax')
47+
container: $(this).attr('data-pjax'),
48+
clickedElement: $(this)
4149
}
4250

4351
$.pjax($.extend({}, defaults, options))
@@ -55,7 +63,7 @@ $.fn.pjax = function( container, options ) {
5563
//
5664
// Accepts these extra keys:
5765
//
58-
// container - Where to stick the response body.
66+
// container - Where to stick the response body. Must be a String.
5967
// $(container).html(xhr.responseBody)
6068
// push - Whether to pushState the URL. Defaults to true (of course).
6169
// replace - Want to use replaceState instead? That's cool.
@@ -73,6 +81,11 @@ $.pjax = function( options ) {
7381
// We don't want to let anyone override our success handler.
7482
delete options.success
7583

84+
// We can't persist $objects using the history API so we must use
85+
// a String selector. Bail if we got anything else.
86+
if ( typeof options.container !== 'string' )
87+
throw "pjax container must be a string selector!"
88+
7689
var defaults = {
7790
timeout: 650,
7891
push: true,
@@ -113,11 +126,6 @@ $.pjax = function( options ) {
113126
timeout: options.timeout
114127
}
115128

116-
// We can't persist $objects using the history API so we need to store
117-
// the string selector.
118-
if ( $.isPlainObject(state.pjax) )
119-
state.pjax = state.pjax.selector
120-
121129
// If there are extra params, save the complete URL in the state object
122130
var query = $.param(options.data)
123131
if ( query != "_pjax=true" )
@@ -174,7 +182,7 @@ var popped = ('state' in window.history), initialURL = location.href
174182
//
175183
// You probably shouldn't use pjax on pages with other pushState
176184
// stuff yet.
177-
$(window).bind('popstate', function(event) {
185+
$(window).bind('popstate', function(event){
178186
// Ignore inital popstate that some browsers fire on page load
179187
var initialPop = !popped && location.href == initialURL
180188
popped = true
@@ -183,11 +191,11 @@ $(window).bind('popstate', function(event) {
183191
var state = event.state
184192

185193
if ( state && state.pjax ) {
186-
var $container = $(state.pjax+'')
187-
if ( $container.length )
194+
var container = state.pjax
195+
if ( $(container+'').length )
188196
$.pjax({
189197
url: state.url || location.href,
190-
container: $container,
198+
container: container,
191199
push: false,
192200
timeout: state.timeout
193201
})
@@ -199,15 +207,20 @@ $(window).bind('popstate', function(event) {
199207

200208
// Add the state property to jQuery's event object so we can use it in
201209
// $(window).bind('popstate')
202-
if ( $.event.props.indexOf('state') < 0 )
210+
if ( $.inArray('state', $.event.props) < 0 )
203211
$.event.props.push('state')
204212

205213

214+
// Is pjax supported by this browser?
215+
$.support.pjax = window.history && window.history.pushState
216+
217+
206218
// Fall back to normalcy for older browsers.
207-
if ( !window.history || !window.history.pushState ) {
208-
$.pjax = $.noop
219+
if ( !$.support.pjax ) {
220+
$.pjax = function( options ) {
221+
window.location = $.isFunction(options.url) ? options.url() : options.url
222+
}
209223
$.fn.pjax = function() { return this }
210224
}
211225

212-
213226
})(jQuery);

0 commit comments

Comments
 (0)