On Github Daniel-Hug / modular-web-apps
git.io/voYWW
Daniel Hug
The goal:
renderItem({ title: 'eggs', quantity: '1 dozen' });
The template:
<ul id="shopping-list"> <script id="item-template" type="text/handlebars"> <li>{{title}}: buy {{quantity}}</li> </script> </ul>
var templateSrc = $('#item-template').html(); var getItemSrc = Handlebars.compile(templateSrc); var $shoppingList = $('#shopping-list'); function renderItem(item) { var itemSrc = getItemSrc(item); $shoppingList.append(itemSrc); }
function renderItem(item) { var $li = $('li').text(item.title + ': buy ' + item.quantity); $shoppingList.append($li); }
function renderItem(item) { var $li = $('<li>'); item.snoop('title quantity', function(title, quantity) { $li.text(title + ': buy ' + quantity); }); $shoppingList.append($li); } var eggs = new Snoopy({ title: 'eggs', quantity: '1 dozen' }); renderItem(eggs);
eggs.set('quantity', '2 dozen');won't refresh entire view.
github.com/daniel-hug/dom-builder
function renderItem(item) { var li = dom({ el: 'li', kids: [item.snoop('title'), ': buy ', item.snoop('quantity')] }); $shoppingList.append(li); }
github.com/daniel-hug/observable-array
// create an observable array var nums = new ObservableArray([1, 2, 3]); // Get notified when it changes nums.on('push', function() { console.log('new values: ' + [].join.call(arguments)); }); // add 2 items nums.push(4, 5);
github.com/daniel-hug/dom-array
// create an observable array var nums = new ObservableArray([1, 2, 3]); var listView = new DomArray(nums, $('ul')[0], function(num) { return $('<li>').text('number: ' + num); }); // add 2 items nums.push(4, 5);