Skip to content

Submitting button value #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ $(document).on('submit', 'form[data-pjax]', function(event) {
})
```

### `$.pjax.submitClick`

Submits a form via pjax. Based on the same code as `$.pjax.submit`, but has the benefit of being able to record the value of the submitting element. Same warnings as `$.pjax.submit` apply.

``` javascript
$(document)
.on('click', 'form :submit', function(event) {
var container = $(this).closest('[data-pjax-container]')
$.pjax.submitClick(event, container)
})
```

### `$.pjax`

Manual pjax invocation. Used mainly when you want to start a pjax request in a handler that didn't originate from a click. If you can get access to a click `event`, consider `$.pjax.click(event)` instead.
Expand Down
75 changes: 69 additions & 6 deletions jquery.pjax.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function handleClick(event, container, options) {
//
// Exported as $.pjax.submit
//
// event - "click" jQuery.Event
// event - "submit" jQuery.Event
// options - pjax options
//
// Examples
Expand All @@ -114,24 +114,85 @@ function handleClick(event, container, options) {
//
// Returns nothing.
function handleSubmit(event, container, options) {
options = optionsFor(container, options)
formSubmissionHandler(event.currentTarget, null, container, options)

event.preventDefault()
}

// Public: pjax on submitting form element handler
//
// Exported as $.pjax.submitClick
//
// event - "click" jQuery.Event
// options - pjax options
// form - target form (optional)
//
// Examples
//
// $(document)
// .on('click', 'form :submit', function(event) {
// var container = $(this).closest('[data-pjax-container]')
// $.pjax.submitClick(event, container)
// })
//
// Returns nothing.
function handleSubmitClick(event, container, options, form) {
if (!form) {
var $parents = $(event.currentTarget).parents('form')

if (!$parents.length)
throw "If submitting element is not contained with target form, please specify form as forth parameter"

form = $(event.currentTarget).parents('form').first()[0]
}

var form = event.currentTarget
formSubmissionHandler(form, event.currentTarget, container, options)

event.preventDefault()
}

// Public: Event handler for handleSubmit & handleSubmitClick
//
// form - target form
// submitter - submitting element (nullable)
// options - pjax options
//
// Returns nothing.
function formSubmissionHandler(form, submitter, container, options) {
options = optionsFor(container, options)

if (form.tagName.toUpperCase() !== 'FORM')
throw "$.pjax.submit requires a form element"

var data = $(form).serializeArray()

// $().serializeArray() does not account for the button pressed
// beause it is not tied to the submit, so the value of a clicked
// submit button must be retrieved manually.
if (submitter) {
var $button = $(submitter)

// Creating and object ot insert in the data array.
var buttonValue = {
name: $button.attr('name'),
value: $button.val()
}

// Only insert if button has a name attribute and a value.
if (buttonValue.name && buttonValue.value) {
data.push(buttonValue)
}
}

var defaults = {
type: form.method.toUpperCase(),
url: form.action,
data: $(form).serializeArray(),
data: data,
container: $(form).attr('data-pjax'),
target: form
}

pjax($.extend({}, defaults, options))

event.preventDefault()
}

// Loads a URL with ajax, puts the response body inside a container,
Expand Down Expand Up @@ -784,6 +845,7 @@ function enable() {
$.pjax.disable = disable
$.pjax.click = handleClick
$.pjax.submit = handleSubmit
$.pjax.submitClick = handleSubmitClick
$.pjax.reload = pjaxReload
$.pjax.defaults = {
timeout: 650,
Expand Down Expand Up @@ -816,6 +878,7 @@ function disable() {
$.pjax.disable = $.noop
$.pjax.click = $.noop
$.pjax.submit = $.noop
$.pjax.submitClick = $.noop
$.pjax.reload = function() { window.location.reload() }

$(window).off('popstate.pjax', onPjaxPopstate)
Expand Down