UNPKG

rule

Version:

Coffeescript friendly css selector based templating

141 lines (121 loc) 2.81 kB
// Generated by CoffeeScript 1.4.0 (function() { var book, itemRule, listItem, listRule, recursive, simple; console.time('simple'); simple = new Rule({ '.content': function() { return this.content; } }); simple.render({ content: 'simple' }, document.querySelector('.simple')); console.timeEnd('simple'); console.time('book'); book = new Rule({ '.title': function() { return this.title; }, '.author': { '.name': { '.first': function() { return this.author.first; }, '.last': function() { return this.author.last; } } } }); book.render({ title: 'The Hobbit', author: { first: 'J. R. R.', last: 'Tolkien' } }, document.querySelector('.book')); console.timeEnd('book'); console.time('list'); itemRule = new Rule({ '.content': function() { var _ref; return (_ref = this.content) != null ? _ref : 'default'; } }, document.querySelector('.list li')); listRule = new Rule({ '': function() { var item, _i, _len, _ref, _results; _ref = this.list; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; _results.push(itemRule.render(item)); } return _results; } }); listRule.render({ list: [ { content: 'hello' }, { content: 'world' }, {} ] }, document.querySelector('.list')); console.timeEnd('list'); console.time('recursive'); listItem = new Rule({ '': function() { return this.location; }, '>': function() { if (this.list != null) { return recursive.render(this); } } }, document.querySelector('.location')); recursive = new Rule({ '': function() { var item, _i, _len, _ref, _results; _ref = this.list; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { item = _ref[_i]; _results.push(listItem.render(item)); } return _results; } }, document.querySelector('.recursive')); recursive.render({ list: [ { location: 'USA', list: [ { location: 'California', list: [ { location: 'San Diego' }, { location: 'San Francisco' }, { location: 'San Jose' } ] }, { location: 'Nevada', list: [ { location: 'Reno' }, { location: 'Las Vegas' } ] } ] } ] }, document.querySelector('.recursive')); console.timeEnd('recursive'); }).call(this);