-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax-inputs.coffee
51 lines (48 loc) · 1.43 KB
/
ajax-inputs.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
$ = jQuery
defaults =
selector: 'input'
events: 'change'
method: 'POST'
# parameters to be sent with all requests
data: {}
dataType: 'json'
namespace = 'ajaxInputs'
$.fn[namespace] =
defaults: defaults
##
# options.url (required) Defines the URL that should be called on change.
# The input name and value attributes will be appended to this URL as name=value
$.fn[namespace] = (option)->
args = arguments
return this.each ->
$this = $(this)
options = option if typeof option == 'object'
data = $this.data(namespace)
if !data then $this.data(namespace, data = new AjaxInputs(this, options))
if typeof option == 'string'
args = Array.prototype.slice.call(arguments, 0)
args.shift()
data[option].apply data, args
class AjaxInputs
constructor: (el, @options)->
@$el = $(el)
@options = $.extend {}, defaults, @options
@$el.on @options.events, @options.selector, $.proxy @onChange, @
onChange: (e)->
$input = $(e.target)
data = $.extend {}, @options.data
data[$input.attr 'name'] = $input.val()
(($input)=>
$.ajax
url: @options.url
data: data
type: @options.method
dataType: @options.dataType
success: (response)=>
if $.fn.formUnload then @$el.closest('form').formUnload('stored', $input)
@$el.trigger "#{namespace}:done", response, e
.fail (jqXHR)=>
@$el.trigger "#{namespace}:fail", jqXHR, e
.always (response)=>
@$el.trigger "#{namespace}:always", response, e
)($input)