Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active October 2, 2015 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balupton/2157598 to your computer and use it in GitHub Desktop.
Save balupton/2157598 to your computer and use it in GitHub Desktop.
Backbone.js introduction for Mickey

Backbone.js introduction for Mickey

Page = Backbone.View.extend({
tagName: 'div',
initialize: function(model,options){
// Chain
return this;
},
render: function(){
// Chain
return this;
}
})
var HomePage = Page.extend({});
var AboutPage = Page.extend({});
/*
.templates {
display:none;
}
<body>
<div class="templates">
<div class="homepage template">
<div class="homepage view">
this is the homepage
</div>
</div>
<div class="aboutpage template">
<div class="aboutpage view">
this is the aboutpage
</div>
</div>
<div class="app template">
<div class="app view">
<div class="homepage wrapper"></div>
<div class="aboutpage wrapper"></div>
<button class="clicker"></div>
</div>
</div>
</div>
<div class="app wrapper"></div>
</body>
<div class="app wrapper">
<div>
<div class="app view">
<div class="homepage wrapper">
<div>
<div class="homepage view">
this is the homepage
</div>
</div>
</div>
<div class="aboutpage wrapper">
<div>
<div class="aboutpage view">
this is the aboutpage
</div>
</div>
</div>
</div>
</div>
</div>
*/
Application = Backbone.View.extend({
tagName: 'div',
subViews: {},
initialize: function(){
// Destroy our references
this.subViews = _.extend({},this.subViews);
// Create our subViews
this.subViews.homePage = new HomePage();
this.subViews.aboutPage = new AboutPage();
// Chain
return this;
},
render: function(){
// Prepare
var me = this;
var homePage = this.subViews.homePage;
var aboutPage = this.subViews.aboutPage;
// Render our template
$('.app.template').clone().appendTo(this.$el);
var $homePageWrapper = this.$('.homepage.wrapper');
var $aboutPageWrapper = this.$('.aboutpage.wrapper')
// Render our subviews to ourself
homePage.render().$el.appendTo($homePageWrapper);
aboutPage.render().$el.appendTo($aboutPageWrapper);
// Bind our events
this.$('.clicker').click(function(){
me.trigger('clickerClick');
});
}
});
// Mediator/Controller
var app = new Application();
app.render().$el.appendTo($('.application.wrapper'));
app.on('newTask',function(task){
alert('you just clicked the clicker');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment