backbone-childs
Advanced tools
+37
| backbone-childs | ||
| =============== | ||
| Extends the default [Backbone view](http://backbonejs.org/#View) with support for subviews (aka child views). | ||
| It uses [backbone-viewj](https://github.com/greenify/bacbone-viewj) (backbone view with jbone) and doesn't need jQuery nor Zepto as dependecy. | ||
| Features | ||
| -------- | ||
| * `ordering`: (optiona) add this attribute to your view for custom ordering (otherwise it will order after your key) | ||
| * `key` access : views are saved in a dictionary | ||
| ### renderSubviews | ||
| call this command in your render function to render all subviews. | ||
| ### addView(view,key) | ||
| adds a view with `key`. | ||
| If you don't have a `ordering` attribute it will set your `key` as ordering attribute. | ||
| ### getView (key) | ||
| gets the view with `key`. | ||
| ### removeView (key) | ||
| removes the view with `key`. | ||
| ### removeViews() | ||
| destroys all child views. | ||
| ### remove() | ||
| destroys this view |
+39
-7
@@ -5,11 +5,39 @@ var _ = require('underscore'); | ||
| /** | ||
| * Remove an element and provide a function that inserts it into its original position | ||
| * @param element {Element} The element to be temporarily removed | ||
| * @return {Function} A function that inserts the element into its original position | ||
| **/ | ||
| function removeToInsertLater(element) { | ||
| var parentNode = element.parentNode; | ||
| var nextSibling = element.nextSibling; | ||
| parentNode.removeChild(element); | ||
| return function() { | ||
| if (nextSibling) { | ||
| parentNode.insertBefore(element, nextSibling); | ||
| } else { | ||
| parentNode.appendChild(element); | ||
| } | ||
| }; | ||
| } | ||
| var removeChilds = function (node) { | ||
| var last; | ||
| while (last = node.lastChild) node.removeChild(last); | ||
| }; | ||
| module.exports = pluginator = viewType.extend({ | ||
| renderSubviews: function() { | ||
| // it is faster to remove the entire element and replace it | ||
| // -> however this will lead to lost id,class and style props | ||
| var oldEl = this.el; | ||
| var el = document.createElement("div"); | ||
| this.setElement(el); | ||
| // it might be that the element is not on the DOM yet | ||
| var elOnDom = oldEl.parentNode != undefined; | ||
| if(elOnDom){ | ||
| var insert = removeToInsertLater(oldEl) | ||
| } | ||
| removeChilds(oldEl); | ||
| var frag = document.createDocumentFragment(); | ||
| if (oldEl.parentNode != null) { | ||
| oldEl.parentNode.replaceChild(this.el, oldEl); | ||
| } | ||
| var views = this._views(); | ||
@@ -28,4 +56,8 @@ var viewsSorted = _.sortBy(views, function(el) { | ||
| } | ||
| el.appendChild(frag); | ||
| return el; | ||
| oldEl.appendChild(frag); | ||
| if(elOnDom){ | ||
| insert(); | ||
| } | ||
| return oldEl; | ||
| }, | ||
@@ -32,0 +64,0 @@ addView: function(key, view) { |
+1
-1
| { | ||
| "name": "backbone-childs", | ||
| "version": "1.0.2", | ||
| "version": "1.0.3", | ||
| "description": "Extended backbone view with support for child views", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
4104
75.68%4
33.33%99
37.5%0
-100%38
Infinity%