-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathbootstrap-editable-rails.js.coffee
More file actions
34 lines (33 loc) · 1.27 KB
/
bootstrap-editable-rails.js.coffee
File metadata and controls
34 lines (33 loc) · 1.27 KB
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
# bootstrap-editable-rails.js.coffee
# Modify parameters of X-editable suitable for Rails.
jQuery ($) ->
EditableForm = $.fn.editableform.Constructor
unless EditableForm::saveWithUrlHook?
EditableForm::saveWithUrlHook = (value) ->
originalUrl = @options.url
resource = @options.resource
@options.url = (params) =>
# TODO: should not send when create new object
if typeof originalUrl == 'function' # user's function
originalUrl.call(@options.scope, params)
else if originalUrl? && @options.send != 'never'
# send ajax to server and return deferred object
obj = {}
obj[params.name] = params.value
# support custom inputtypes (eg address)
if resource
params[resource] = obj
else
params = obj
delete params.name
delete params.value
delete params.pk
$.ajax($.extend({
url : originalUrl
data : params
type : 'PATCH' # TODO: should be 'POST' when create new object
dataType: 'json'
}, @options.ajaxOptions))
@saveWithoutUrlHook(value)
EditableForm::saveWithoutUrlHook = EditableForm::save
EditableForm::save = EditableForm::saveWithUrlHook