|
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; |
|
}); |