// ProductList Module var productListModule = (function(postal, $) { var prodListTemplate = "#product-list-tmpl", prodItemSelector = "#products li", wireProductList = function(prodList) { postal.subscribe("productlist", "init", _.bind(prodList.init, prodList)); postal.subscribe("productlist", "populate", function(data) { _.each(data.items, function(item) { prodList.items.push(new Product(item.id, item.description, item.price)); }); prodList.proxy.render(prodList); }); }, ProductListPrototype = { init: function() { this.items = []; postal.publish("productlist", "request.getproductlist", {}); } }, ProductList = function(domProxy) { this.items = []; this.proxy = domProxy; wireProductList(this); }, ProductPrototype = { onSelect: function() { postal.publish("productlist", "item.selected", this); } }, Product = function(id, desc, price) { this.id = id; this.description = desc; this.price = price; }, ProductListDomProxyPrototype = { render: function(model) { var domSel = $(this.selector); domSel.html(_.template($(prodListTemplate).html(), model)); _.each(model.items, function(item) { $("#" + item.id).click(_.bind(item.onSelect, item)); }); $(prodItemSelector) .append("click to add") .hover( function( e ) { $( this ).children( ".tool-tip" ).toggle( e.type === "mouseenter" ); }); } }, ProductListDomProxy = function(selector) { this.selector = selector; }; ProductList.prototype = ProductListPrototype; Product.prototype = ProductPrototype; ProductListDomProxy.prototype = ProductListDomProxyPrototype; return { start: function(selector) { return new ProductList(new ProductListDomProxy(selector)); } } })(postal, jQuery); // END ProductList Module