2013-06-22 06:24:24 +00:00
|
|
|
'use strict';
|
2013-09-11 21:38:35 +00:00
|
|
|
define(function () {
|
2013-06-24 23:41:59 +00:00
|
|
|
//This module will automatically route all relative links through backbone router rather than
|
2013-06-06 05:13:31 +00:00
|
|
|
//causing links to reload pages.
|
|
|
|
|
|
|
|
var routeBinder = {
|
|
|
|
|
2013-06-21 01:43:58 +00:00
|
|
|
bind: function (router) {
|
2013-06-21 05:19:41 +00:00
|
|
|
var self = this;
|
|
|
|
$(document).on('click', 'a[href]', function (event) {
|
|
|
|
self._handleClick(event, router);
|
|
|
|
});
|
2013-06-07 00:17:57 +00:00
|
|
|
},
|
|
|
|
|
2013-06-21 05:19:41 +00:00
|
|
|
_handleClick: function (event, router) {
|
2013-06-06 05:13:31 +00:00
|
|
|
var $target = $(event.target);
|
|
|
|
|
2013-06-07 00:17:57 +00:00
|
|
|
//check if tab nav
|
|
|
|
if ($target.parents('.nav-tabs').length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-06 05:13:31 +00:00
|
|
|
if ($target.hasClass('no-router')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var href = event.target.getAttribute('href');
|
|
|
|
|
|
|
|
if (!href && $target.parent('a') && $target.parent('a')[0]) {
|
|
|
|
|
|
|
|
var linkElement = $target.parent('a')[0];
|
|
|
|
|
|
|
|
href = linkElement.getAttribute('href');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!href) {
|
2013-06-24 23:41:59 +00:00
|
|
|
throw 'couldn\'t find route target';
|
2013-06-06 05:13:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-11 21:38:35 +00:00
|
|
|
if (!href.startsWith('http')) {
|
2013-06-21 05:19:41 +00:00
|
|
|
router.navigate(href, { trigger: true });
|
2013-06-06 05:13:31 +00:00
|
|
|
}
|
2013-06-07 15:42:49 +00:00
|
|
|
|
|
|
|
else {
|
|
|
|
//Open in new tab
|
|
|
|
window.open(href, '_blank');
|
|
|
|
}
|
2013-06-06 05:13:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return routeBinder;
|
|
|
|
});
|