1
1
// jquery.pjax.js
2
2
// copyright chris wanstrath
3
- // https://github.com/defunkt/pjax
3
+ // https://github.com/defunkt/jquery- pjax
4
4
5
5
( function ( $ ) {
6
6
@@ -29,6 +29,13 @@ $.fn.pjax = function( container, options ) {
29
29
else
30
30
options = $ . isPlainObject ( container ) ? container : { container :container }
31
31
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
+
32
39
return this . live ( 'click' , function ( event ) {
33
40
// Middle click, cmd click, and ctrl click should open
34
41
// links in a new tab as normal.
@@ -37,7 +44,8 @@ $.fn.pjax = function( container, options ) {
37
44
38
45
var defaults = {
39
46
url : this . href ,
40
- container : $ ( this ) . attr ( 'data-pjax' )
47
+ container : $ ( this ) . attr ( 'data-pjax' ) ,
48
+ clickedElement : $ ( this )
41
49
}
42
50
43
51
$ . pjax ( $ . extend ( { } , defaults , options ) )
@@ -55,7 +63,7 @@ $.fn.pjax = function( container, options ) {
55
63
//
56
64
// Accepts these extra keys:
57
65
//
58
- // container - Where to stick the response body.
66
+ // container - Where to stick the response body. Must be a String.
59
67
// $(container).html(xhr.responseBody)
60
68
// push - Whether to pushState the URL. Defaults to true (of course).
61
69
// replace - Want to use replaceState instead? That's cool.
@@ -73,6 +81,11 @@ $.pjax = function( options ) {
73
81
// We don't want to let anyone override our success handler.
74
82
delete options . success
75
83
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
+
76
89
var defaults = {
77
90
timeout : 650 ,
78
91
push : true ,
@@ -113,11 +126,6 @@ $.pjax = function( options ) {
113
126
timeout : options . timeout
114
127
}
115
128
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
-
121
129
// If there are extra params, save the complete URL in the state object
122
130
var query = $ . param ( options . data )
123
131
if ( query != "_pjax=true" )
@@ -174,7 +182,7 @@ var popped = ('state' in window.history), initialURL = location.href
174
182
//
175
183
// You probably shouldn't use pjax on pages with other pushState
176
184
// stuff yet.
177
- $ ( window ) . bind ( 'popstate' , function ( event ) {
185
+ $ ( window ) . bind ( 'popstate' , function ( event ) {
178
186
// Ignore inital popstate that some browsers fire on page load
179
187
var initialPop = ! popped && location . href == initialURL
180
188
popped = true
@@ -183,11 +191,11 @@ $(window).bind('popstate', function(event) {
183
191
var state = event . state
184
192
185
193
if ( state && state . pjax ) {
186
- var $ container = $ ( state . pjax + '' )
187
- if ( $container . length )
194
+ var container = state . pjax
195
+ if ( $ ( container + '' ) . length )
188
196
$ . pjax ( {
189
197
url : state . url || location . href ,
190
- container : $ container,
198
+ container : container ,
191
199
push : false ,
192
200
timeout : state . timeout
193
201
} )
@@ -199,15 +207,20 @@ $(window).bind('popstate', function(event) {
199
207
200
208
// Add the state property to jQuery's event object so we can use it in
201
209
// $(window).bind('popstate')
202
- if ( $ . event . props . indexOf ( 'state' ) < 0 )
210
+ if ( $ . inArray ( 'state' , $ . event . props ) < 0 )
203
211
$ . event . props . push ( 'state' )
204
212
205
213
214
+ // Is pjax supported by this browser?
215
+ $ . support . pjax = window . history && window . history . pushState
216
+
217
+
206
218
// 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
+ }
209
223
$ . fn . pjax = function ( ) { return this }
210
224
}
211
225
212
-
213
226
} ) ( jQuery ) ;
0 commit comments