Skip to content

Instantly share code, notes, and snippets.

@c4urself
Created August 4, 2012 17:43
Show Gist options
  • Save c4urself/3258905 to your computer and use it in GitHub Desktop.
Save c4urself/3258905 to your computer and use it in GitHub Desktop.
Topnav backbone view
define([
'jquery',
'underscore',
'views/abstractview',
'models/authentication',
'text!templates/navigation/topnav.html',
'text!templates/navigation/notauthenticated.html'
], function($, _, AbstractView, authModel, mainNavigationTemplate, notAuthenticatedTemplate){
var view = AbstractView.extend({
events: {
'click button.signout': 'doSignout'
},
doSignout: function (e) {
console.log(e);
console.log(e.currentTarget);
conosle.log($(e.currentTarget));
authModel.logout();
},
initializeCallback: function(options) {
if (authModel.isUserLoggedIn()) {
this.template = mainNavigationTemplate;
this.options.router.bind('all', this.routeChanged, this);
this.render();
} else {
this.renderView(this.el, notAuthenticatedTemplate, null);
}
},
routeChanged: function() {
var found = false;
var newRoute = document.location.hash;
var navLink, fragmentIndex;
// This is much quicker than $("li:has(>a)").removeClass('selected');
this.$("div > a").removeClass('selected');
do {
navLink = this.$('div > a[href="' + newRoute + '"]');
if (navLink.length === 1) {
found = true;
navLink.addClass('selected');
} else {
fragmentIndex = newRoute.lastIndexOf('/');
if (fragmentIndex !== -1) {
newRoute = newRoute.substring(0, fragmentIndex);
} else {
found = true; //avoid infinite loop
}
}
}
while (!found);
},
render: function (){
this.renderView();
// update selected route
this.routeChanged();
this.$(".name").html(authModel.get("authname"));
},
});
return view;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment