Skip to content

Commit 6fc6bd6

Browse files
committed
Add pjax form submit helper
1 parent 4040ed1 commit 6fc6bd6

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

jquery.pjax.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ function fnPjax(container, options) {
4444
//
4545
// $(document).on('click', 'a', function(event) {
4646
// var container = $(this).closest('[data-pjax-container]')
47-
// return $.pjax.click(event, container)
47+
// $.pjax.click(event, container)
4848
// })
4949
//
50-
// Returns false if pjax runs, otherwise nothing.
50+
// Returns nothing.
5151
function handleClick(event, container, options) {
5252
options = optionsFor(container, options)
5353

@@ -87,6 +87,48 @@ function handleClick(event, container, options) {
8787
event.preventDefault()
8888
}
8989

90+
// Public: pjax on form submit handler
91+
//
92+
// Exported as $.pjax.submit
93+
//
94+
// event - "click" jQuery.Event
95+
// options - pjax options
96+
//
97+
// Examples
98+
//
99+
// $(document).on('submit', 'form', function(event) {
100+
// var container = $(this).closest('[data-pjax-container]')
101+
// $.pjax.submit(event, container)
102+
// })
103+
//
104+
// Returns nothing.
105+
function handleSubmit(event, container, options) {
106+
options = optionsFor(container, options)
107+
108+
var form = event.currentTarget
109+
110+
if (form.tagName.toUpperCase() !== 'FORM')
111+
throw "$.pjax.submit requires a form element"
112+
113+
// Ignore cross origin links
114+
if ( location.protocol !== link.protocol || location.host !== link.host )
115+
return
116+
117+
var defaults = {
118+
type: form.method,
119+
url: form.action,
120+
data: $(form).serializeArray()
121+
container: $(form).attr('data-pjax'),
122+
target: form,
123+
fragment: null,
124+
timeout: 0
125+
}
126+
127+
pjax($.extend({}, defaults, options))
128+
129+
event.preventDefault()
130+
}
131+
90132
// Loads a URL with ajax, puts the response body inside a container,
91133
// then pushState()'s the loaded URL.
92134
//

0 commit comments

Comments
 (0)