Last active
August 29, 2015 14:06
-
-
Save jmas/4848ac6620a5042204a5 to your computer and use it in GitHub Desktop.
Component List. Is an example of using Arr.js. Require micromustache template engine, throttle.js utility.
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
var List = function(data, el, itemTamplate) { | |
if (! data instanceof Arr) { | |
throw new Error('data should be an instance of Arr'); | |
} | |
if (! el instanceof HTMLElement) { | |
throw new Error('el should be an instanceof of HTMLElement'); | |
} | |
if (typeof itemTamplate !== 'string') { | |
throw new Error('itemTamplate should be a string'); | |
} | |
if (typeof micromustache === 'undefined') { | |
throw new Error('micromustache (global) library is required'); | |
} | |
if (typeof throttle === 'undefined') { | |
throw new Error('throttle (global) library is required'); | |
} | |
this.data = data; | |
this.el = el; | |
this.itemTamplate = itemTamplate; | |
this.data.on('change', throttle(this.render.bind(this), 300)); | |
if (this.data.length > 0) { | |
this.render(); | |
} | |
}; | |
List.prototype = { | |
// | |
clean: function() { | |
while (this.el.firstChild) { | |
this.el.removeChild(this.el.firstChild); | |
} | |
return this; | |
}, | |
// | |
render: function() { | |
this.clean(); | |
if (this.data.length > 0) { | |
for (var i=0,len=this.data.length; i<len; i++) { | |
this.el.insertAdjacentHTML('beforeend', micromustache.render(this.itemTamplate, this.data[i])); | |
} | |
} | |
return this; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment