Skip to content

Instantly share code, notes, and snippets.

@mstapp
Created March 9, 2014 18:22
Show Gist options
  • Save mstapp/9452045 to your computer and use it in GitHub Desktop.
Save mstapp/9452045 to your computer and use it in GitHub Desktop.
blog - attach view to html
// attach to static html at element "#navbar-links"
var myview = new MyView( {el: $('#navbar-links')} );
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">My Application</a>
</div>
<div class="collapse navbar-collapse" id="navbar-links">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Monkeys</a></li>
<li><a href="#gorillas">Gorillas</a></li>
</ul>
</div>
</div>
</nav>
Module.Controller = Marionette.Controller.extend({
start: function(){
this.view = new Module.View( {el: $('#navbar-links')} ); // attach to static html @ that element
}
});
Module.View = Marionette.ItemView.extend({
// override: don't really render, since this view just attaches to existing navbar html.
render: function() {
}
});
Module.View = Marionette.ItemView.extend({
events: {
'click a': 'onClickLink',
},
onClickLink: function(e) {
this.$('li.active').toggleClass('active', false); // turn previously-selected nav link off
$(e.target).blur()
.closest('li').toggleClass('active', true); // turn on new link
},
// custom render: don't really render, since this view just attaches to existing navbar html.
render: function() {
},
});
// override: don't really render, since this view just attaches to existing navbar html.
render: function() {
this.bindUIElements(); // wire up this.ui, if any.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment