Skip to content

Commit

Permalink
ionTab uses session, localStorage, and router fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwoody committed Jan 10, 2015
1 parent bbc9bd4 commit 093ae32
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
29 changes: 27 additions & 2 deletions components/ionTab/ionTab.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
Tracker.autorun(function () {
var ionTabCurrent = Session.get('ionTab.current');

if( ionTabCurrent ){
localStorage.setItem('ionTab.current', ionTabCurrent);
}
});

Meteor.startup(function () {
var ionTabCurrent = localStorage.getItem('ionTab.current');

if( ionTabCurrent ){
Session.set('ionTab.current', ionTabCurrent);
}
});

Template.ionTab.events({
'click': function (event, template) {
if (template.data.path) {
Session.set('currentTab', template.data.path);
Session.set('ionTab.current', template.data.path);

This comment has been minimized.

Copy link
@nickw

nickw Jan 10, 2015

Ah good idea :)

}

// If the tab's content is being rendered inside of a ionNavView
Expand All @@ -22,7 +38,16 @@ Template.ionTab.helpers({
},

isActive: function () {
if (this.path && this.path === Session.get('currentTab')) {
var ionTabCurrent = Session.get('ionTab.current');

if (this.path && this.path === ionTabCurrent) {
return 'active';
}

// The initial case where there is no localStorage value and
// no session variable has been set, this attempts to set the correct tab
// to active based on the router
if(Router.routes[this.path].path(Template.parentData(1)) === ionTabCurrent){
return 'active';
}
},
Expand Down
13 changes: 13 additions & 0 deletions components/ionTabs/ionTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ Template.ionTabs.rendered = function () {
} else {
Session.set('hasTabs', true);
}

// This is a fallback if no localStorage is found:
// look through tabs and see if current route matches
// one of the href attributes, cross fingers
if( !Session.get('ionTab.current') ){
this.$('.tabs').children().each(function() {
var href = $(this).attr('href');
var current = Router.current().route.path();
if(href === current){
Session.set('ionTab.current', href);
}
});
}
};

Template.ionTabs.destroyed = function () {
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Package.describe({

Package.onUse(function(api) {
api.versionsFrom("1.0");
api.use(["templating", "underscore", "fastclick", "iron:router"], "client");
api.use(["templating", "underscore", "fastclick", "iron:router", "tracker", "session"], "client");

api.addFiles([
"vendor/snap.js",
Expand Down

0 comments on commit 093ae32

Please sign in to comment.