Skip to content

Instantly share code, notes, and snippets.

@lushchick
Last active December 14, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lushchick/5056143 to your computer and use it in GitHub Desktop.
Save lushchick/5056143 to your computer and use it in GitHub Desktop.
var origBind = rivets.binders['each-*'].bind,
col = { attr: 'data-col', idx: 0 },
dataKey = 'rivetsEachData';
/**
* Patch bind method to auto-refresh re-bound data
*
* @todo: remove it when this is fixed in Rivets:
* https://github.com/mikeric/rivets/issues/118
*
* @param el
* @param collection
* @return {Object}
*/
rivets.binders['each-*'].bind = function(el, collection) {
var $el = $(el), data = {},
attrName = ['data', rivets.config.prefix, this.type].join('-').replace('--', '-');
data[attrName] = $el.attr(attrName);
$el.data(dataKey, data);
if ($el.attr(col.attr) === undefined) {
$el.attr(col.attr, col.idx++);
}
return origBind.apply(this, Array.prototype.slice.call(arguments));
};
/**
* Patch unbind method to clear previously bound data and turn DOM
* into the original state (with all data- attributes)
*
* @todo: remove it when this is fixed in Rivets
* https://github.com/mikeric/rivets/issues/118
*
* @param el
* @param collection
*/
rivets.binders['each-*'].unbind = function(el, collection) {
var $el = $(el),
attr = $el.attr(col.attr);
$el.attr($el.data(dataKey));
$('[' + col.attr + ']', this.marker.parentNode)
.filter(function() {
return $(this).attr(col.attr) === attr;
})
.first().before(el).end().remove();
$(this.marker).remove();
};
@redguardtoo
Copy link

where you place the code, you need patch rivets.js?
Could you send me a complete example and patched rivets.js

@lushchick
Copy link
Author

You may put it somewhere before you initialize adapters using rivets.configure (say, after you include rivets.js into your page)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment