Skip to content

Commit d899eaa

Browse files
committed
Merge pull request #386 from defunkt/event-docs
Improve documentation about pjax events
2 parents 97bccf9 + 2fd2560 commit d899eaa

File tree

1 file changed

+101
-23
lines changed

1 file changed

+101
-23
lines changed

README.md

Lines changed: 101 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -187,28 +187,105 @@ function applyFilters() {
187187

188188
### Events
189189

190-
pjax fires a number of events regardless of how its invoked.
191-
192-
All events are fired from the container, not the link was clicked.
193-
194-
#### start and end
195-
196-
* `pjax:start` - Fired when pjaxing begins.
197-
* `pjax:end` - Fired when pjaxing ends.
198-
* `pjax:click` - Fired when pjaxified link is clicked.
199-
200-
This pair events fire anytime a pjax request starts and finishes. This includes pjaxing on `popstate` and when pages are loaded from cache instead of making a request.
201-
202-
#### ajax related
203-
204-
* `pjax:beforeSend` - Fired before the pjax request begins. Returning false will abort the request.
205-
* `pjax:send` - Fired after the pjax request begins.
206-
* `pjax:complete` - Fired after the pjax request finishes.
207-
* `pjax:success` - Fired after the pjax request succeeds.
208-
* `pjax:error` - Fired after the pjax request fails. Returning false will prevent the the fallback redirect.
209-
* `pjax:timeout` - Fired if after timeout is reached. Returning false will disable the fallback and will wait indefinitely until the response returns.
210-
211-
`send` and `complete` are a good pair of events to use if you are implementing a loading indicator. They'll only be triggered if an actual request is made, not if it's loaded from cache.
190+
All pjax events except `pjax:click` & `pjax:clicked` are fired from the pjax
191+
container, not the link that was clicked.
192+
193+
<table>
194+
<tr>
195+
<th>event</th>
196+
<th>cancel</th>
197+
<th>arguments</th>
198+
<th>notes</th>
199+
</tr>
200+
<tr>
201+
<th colspan=4>event lifecycle upon following a pjaxed link</th>
202+
</tr>
203+
<tr>
204+
<td><code>pjax:click</code></td>
205+
<td>✔︎</td>
206+
<td><code>options</code></td>
207+
<td>fires from a link that got activated; cancel to prevent pjax</td>
208+
</tr>
209+
<tr>
210+
<td><code>pjax:beforeSend</code></td>
211+
<td>✔︎</td>
212+
<td><code>xhr, options</code></td>
213+
<td>can set XHR headers</td>
214+
</tr>
215+
<tr>
216+
<td><code>pjax:start</code></td>
217+
<td></td>
218+
<td><code>xhr, options</code></td>
219+
<td></td>
220+
</tr>
221+
<tr>
222+
<td><code>pjax:send</code></td>
223+
<td></td>
224+
<td><code>xhr, options</code></td>
225+
<td></td>
226+
</tr>
227+
<tr>
228+
<td><code>pjax:clicked</code></td>
229+
<td></td>
230+
<td><code>options</code></td>
231+
<td>fires after pjax has started from a link that got clicked</td>
232+
</tr>
233+
<tr>
234+
<td><code>pjax:success</code></td>
235+
<td></td>
236+
<td><code>data, status, xhr, options</code></td>
237+
<td>after replacing HTML content loaded from the server</td>
238+
</tr>
239+
<tr>
240+
<td><code>pjax:timeout</code></td>
241+
<td>✔︎</td>
242+
<td><code>xhr, options</code></td>
243+
<td>fires after <code>options.timeout</code>; will hard refresh unless canceled</td>
244+
</tr>
245+
<tr>
246+
<td><code>pjax:error</code></td>
247+
<td>✔︎</td>
248+
<td><code>xhr, textStatus, error, options</code></td>
249+
<td>on ajax error; will hard refresh unless canceled</td>
250+
</tr>
251+
<tr>
252+
<td><code>pjax:complete</code></td>
253+
<td></td>
254+
<td><code>xhr, textStatus, options</code></td>
255+
<td>always fires after ajax, regardless of result</td>
256+
</tr>
257+
<tr>
258+
<td><code>pjax:end</code></td>
259+
<td></td>
260+
<td><code>xhr, options</code></td>
261+
<td></td>
262+
</tr>
263+
<tr>
264+
<th colspan=4>event lifecycle on browser Back/Forward navigation</th>
265+
</tr>
266+
<tr>
267+
<td><code>pjax:popstate</code></td>
268+
<td></td>
269+
<td></td>
270+
<td>event <code>direction</code> property: &quot;back&quot;/&quot;forward&quot;</td>
271+
</tr>
272+
<tr>
273+
<td><code>pjax:start</code></td>
274+
<td></td>
275+
<td><code>null, options</code></td>
276+
<td>before replacing content</td>
277+
</tr>
278+
<tr>
279+
<td><code>pjax:end</code></td>
280+
<td></td>
281+
<td><code>null, options</code></td>
282+
<td>after replacing content</td>
283+
</tr>
284+
</table>
285+
286+
`pjax:send` & `pjax:complete` are a good pair of events to use if you are implementing a
287+
loading indicator. They'll only be triggered if an actual XHR request is made,
288+
not if the content is loaded from cache:
212289

213290
``` javascript
214291
$(document).on('pjax:send', function() {
@@ -219,7 +296,8 @@ $(document).on('pjax:complete', function() {
219296
})
220297
```
221298

222-
Another protip: disable the fallback timeout behavior if a spinner is being shown.
299+
An example of canceling a `pjax:timeout` event would be to disable the fallback
300+
timeout behavior if a spinner is being shown:
223301

224302
``` javascript
225303
$(document).on('pjax:timeout', function(event) {

0 commit comments

Comments
 (0)