Last active
August 2, 2016 16:33
-
-
Save bruncun/de1f6ab37e80d84f8b3ee99c155771ae to your computer and use it in GitHub Desktop.
Render Partials for jQuery UJS
This file contains 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
(function () { | |
var partials = { | |
avatar: '<%= j render partial: "avatar", locals: { avatar: @profile.avatar } %>', | |
avatarForm: '<%= j render partial: "form" %>' | |
} | |
$.rails.renderPartials(partials); | |
})(); |
This file contains 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
function (str, sep) { | |
if (typeof str !== 'string') { | |
throw new TypeError('Expected a string'); | |
} | |
sep = typeof sep === 'undefined' ? '_' : sep; | |
return str | |
.replace(/([a-z\d])([A-Z])/g, '$1' + sep + '$2') | |
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + sep + '$2') | |
.toLowerCase(); | |
}; |
This file contains 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
//= require decamelize | |
/** | |
* Extend jquery-ujs to ease updating partial renders | |
*/ | |
$.rails.renderPartials = (function () { | |
/** | |
* Look up partial's wrapper | |
* @param partial | |
* @returns {*|jQuery|HTMLElement} | |
* @private | |
*/ | |
function _getPartialElement (partial) { return $('[data-partial~=' + decamelize(partial, '-') + ']'); } | |
/** | |
* If prop is a partial, render it | |
* @param prop | |
* @param partials | |
* @private | |
*/ | |
function _checkForPartial (prop, partials) { | |
var isPartial = partials.hasOwnProperty(prop); | |
if (isPartial) { _renderPartial(prop, partials); } | |
} | |
/** | |
* Get partial's wrapper and set its HTML content | |
* @param prop | |
* @param partials | |
* @private | |
*/ | |
function _renderPartial (partial, partials) { | |
var $el = _getPartialElement(partial), | |
partialHtml = partials[partial]; | |
$el.html(partialHtml); | |
} | |
/** | |
* Enumerates through properties, checking for partials | |
* @param partials | |
*/ | |
function renderPartials (partials) { for (var prop in partials) { _checkForPartial(prop, partials); } } | |
return renderPartials; | |
})(); |
This file contains 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
%div{ data: { partial: "avatar" } }= render partial: "avatars/avatar", locals: { avatar: @profile.avatar } | |
%div{ data: { partial: "avatar-form" } }= render partial: "avatars/form" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment