On Github wubenhe / mkpbackbone
<article> <div class="container-fluid"> <div class="row-fluid"> <nav id="sub_menu" class="span3"> </nav><!--/span--> <section id="content" class="span9"> </section> </div> </div> <hr> </article> <script> C('global'); $(function() { C.global.pageObject = new C.Router.Sample(); }); </script>
C.utility.backbone('Router.Sample')(C.Backbone.Router.extend({ // Routes is a key-pair value array/JSON that indicate routes: { "": "home", "alias": "alias", 'form' : 'form' }, // Executes everytime class is instantiated, similar to a constructor. initialize: function () { ... }, // Callbacks for router. home: function () { ... }, alias : function() { ... }, form : function() { ... } }));
<article> <div class="container-fluid"> <div class="row-fluid"> <nav id="sub_menu" class="span3"> </nav><!--/span--> <section id="content" class="span9"> </section> </div> </div> <hr> </article> <script> C('global'); $(function() { C.global.pageObject = new C.Router.Sample(); }); </script>
C.utility.backbone('Router.Sample')(C.Backbone.Router.extend({ ... initialize: function () { var menuModel = new C.Backbone.Model({ menuData : [{ subtitle : 'sub-menu', links : [{title:'Home Page', href:''},{title:'Alias Page', href:'alias'}] },{ links : [{title:'Backbone form', href:'form'},{title:'Alias Page 2', href:'alias2'}] }] }); this.menuView = new C.View.Menu({el:'#sub_menu', model:menuModel}); this.menuView. render(); }); ... })); C.utility.backbone('View.Menu')(C.Backbone.View.extend({ menu: _.template('<div class="well sidebar-nav">' + '<ul class="nav nav-list">' + '</ul>' + '</div>'), subtitle : _.template('<li class="nav-header"><%= subtitle %></li>'), item : _.template('<li><a href="#<%- href %>"><%= title %></a></li>'), divider : _.template('<li class="divider"></li>'), initialize: function () { Backbone.history.on("route", function() { this.autoHighlights(); }, this); }, autoHighlights: function() { ... }, render: function () { $(this.el).html(this.menu()); var ul = $(this.el).find('ul'); var self = this; _.each(this.model.get('menuData'), function(data) { if(!_.isEmpty(data.subtitle)){ ul.append(self.subtitle(data)); } else{ ul.append(self.divider()); } _.each(data.links, function(data) { ul.append(self.item(data)); }); }); return this; } ... }));
form : function(){ //Main model definition C.utility.backbone('Model.User')(C.Backbone.Model.extend({ schema: { title: { type: 'Select', options: ['', 'Mr', 'Mrs', 'Ms'] }, name: 'Text', email: { validators: ['required', 'email'] }, birthday: 'Date', notes: { type: 'List' }, weapons: { type: 'List', itemType: 'Object', subSchema: { name: { validators: ['required'] }, number: 'Number' }}, search : {type:'Search'} } })); var user = new C.Model.User({ title: 'Mr', name: 'Ben He Wu', email: 'ben@chegg.com', birthday: new Date(1982, 12, 30), ... weapons: [ { name: 'Uzi', number: 2 }, { name: 'Shotgun', number: 1 } ] }); //The form var form = new C.Backbone.Form({ model: user }); form.render(); $('#content').html(form.el); }