Last active
December 14, 2015 08:38
-
-
Save clowder/5058797 to your computer and use it in GitHub Desktop.
A little javascript (script) to give you attribute updating with `prompt` & Rails UJS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jQuery = jQuery, | |
rails = jQuery.rails, | |
document = document; | |
(function($, rails, document) { | |
"use strict"; | |
var PromptUpdate = { | |
defaults: { name: 'value', method: 'put' }, | |
prompt: function(element) { | |
var $element = $(element), | |
options = $.extend({}, this.defaults, $element.data(), { name: $element.attr('name') }), | |
data = prompt(options.prompt); | |
if (data) { | |
$element.data('params', options.name + '=' + data); | |
$element.data('method', options.method); | |
rails.handleRemote($element); | |
} | |
}, | |
_find: function(fn) { | |
return function(list) { | |
var i=list.length; | |
while(i--) { if (fn(list[i])) { return list[i]; } } | |
}; | |
}, | |
_newRailsSelector: function() { | |
var appendNot = function(x) { return x + ':not([data-prompt])'; }; | |
return rails.linkClickSelector.split(',').map(appendNot).join(','); | |
}, | |
moveRailsSelector: function() { | |
var eventName = 'click.rails', | |
selector = rails.linkClickSelector, | |
handlerSearch = this._find(function(x) { return x.origType + '.' + x.namespace === eventName && x.selector === selector; }), | |
handler = handlerSearch($._data(document, 'events').click).handler, | |
newSelector = this._newRailsSelector(); | |
$(document).off(eventName, selector); | |
$(document).on(eventName, newSelector, handler); | |
} | |
}; | |
PromptUpdate.moveRailsSelector(); | |
$(document).on('click', 'a[data-prompt]', function(e) { | |
e.preventDefault(); | |
e.stopImmediatePropagation(); | |
PromptUpdate.prompt(this); | |
}); | |
}(jQuery, rails, document)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YourController < ActionController::Base | |
def update | |
user = User.find(params[:id) | |
if user.update_attributes(post_params) | |
# probably return `something.js.erb` ... | |
end | |
end | |
private | |
def post_params | |
params[:user].slice(:name) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
= link_to 'Change Name', user_path(user), :name => 'user[name]', 'data-prompt' => 'New name?' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment