Skip to content

Commit 32b402d

Browse files
committed
Fixed handler event
1 parent 9d135e0 commit 32b402d

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,19 @@ HTML:
324324
For create a custom responses for Pjax you can use `handler` event, example:
325325

326326
```javascript
327-
Pjax.start({
328-
updatehead: false //Prevent remove itens in head
329-
});
327+
Pjax.start();
328+
329+
Pjax.on("handler", function (details, done, fail) {
330+
console.log("handler request:", details.method, details.url);
331+
console.log("handler target:", details.element);
332+
console.log("handler mode history:", details.state); // 1 == push, 2 == replace
330333

331-
Pjax.on("handler", function (hdata, config, done, fail) {
332334
setTimeout(function () {
333-
done(hdata.url, '<div id="pjax-container">Foo: ' + new Date() + '</div>', config, hdata.state);
335+
if (<condition for history state>) {
336+
done('<div id="pjax-container">Foo: ' + new Date() + '</div>');
337+
} else {
338+
fail("Custom Error");
339+
}
334340
}, 1000);
335341
});
336342
```

examples/www/handler.html

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Handler with Pjax</title>
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, shrink-to-fit=no">
7+
<link rel="shortcut icon" href="/icons/redirect.ico">
8+
<link rel="stylesheet" type="text/css" href="assets/pjax.css" data-pjax-resource="true">
9+
<script src="assets/pjax.js" data-pjax-resource="true"></script>
10+
<script data-pjax-resource="true">
11+
Pjax.start();
12+
13+
Pjax.on("handler", function (details, done, fail) {
14+
console.log("handler request:", details.method, details.url);
15+
console.log("handler target:", details.element);
16+
console.log("handler mode history:", details.state); // 1 == push, 2 == replace
17+
18+
setTimeout(function () {
19+
if (details.url.indexOf("/error.html") !== -1) {
20+
fail("Custom Error");
21+
} else {
22+
done('<div id="pjax-container">Foo: ' + new Date() + '</div>');
23+
}
24+
}, 1000);
25+
});
26+
</script>
27+
<style type="text/css">
28+
#notice {
29+
background-color: #188abd;
30+
color: #fff;
31+
padding: 5px 15px;
32+
}
33+
</style>
34+
</head>
35+
<body>
36+
37+
<script type="text/javascript">
38+
if (!Pjax.supported) {
39+
document.write('<div id="notice">');
40+
document.write('<p>Your browser don\'t support Pjax, using normal navigation</p>');
41+
document.write('</div>');
42+
}
43+
</script>
44+
45+
<ul>
46+
<li><a href="foo.html"><strong>foo.html</strong></a></li>
47+
<li><a href="bar.html"><strong>bar.html</strong></a></li>
48+
<li><a href="baz.html"><strong>baz.html</strong></a></li>
49+
<li><a href="error.html"><strong>error.html</strong></a></li>
50+
</ul>
51+
<div id="pjax-container">
52+
<h1>Simulate navigation</h1>
53+
</div>
54+
</body>
55+
</html>

pjax.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,11 @@
350350
state: state,
351351
method: method,
352352
element: el
353-
}, cfg, pjaxResolve);
353+
}, function (content) {
354+
pjaxResolve(url, cfg, state, el, cfg.done, content, 0, undef);
355+
}, function (error) {
356+
pjaxResolve(url, cfg, state, el, cfg.fail, "", -1, error);
357+
});
354358
}
355359

356360
var reqUrl = url, headers = cfg.headers;

pjax.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)