Last active
August 29, 2015 14:16
-
-
Save raix/2473fb7d3fd071251ea2 to your computer and use it in GitHub Desktop.
A ultra simple view handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Meteor.isClient) { | |
// Basic animation handling | |
var defer = function(current, last, scroll) { | |
// Set the hide class and then remove the show | |
$('#'+last).addClass('hide').removeClass('show'); | |
// Remove the hide - this will make the transition fast | |
$('#'+current).removeClass('hide'); | |
Meteor.setTimeout(function() { | |
$('#'+current).addClass('show'); | |
if (scroll) scroll.refresh(); | |
}, 100); | |
}; | |
// Create a view with a name | |
View = function(name) { | |
var self = this; | |
self._current = name; | |
self._prev = name; | |
self._time = new Date(); | |
self._deps = new Tracker.Dependency(); | |
}; | |
View.prototype.set = function(name, toggle) { | |
var self = this; | |
if (new Date() - self._time < 100) return; | |
self._time = new Date(); | |
if (name == self._current) { | |
if (toggle) { | |
// Go back | |
name = self._prev; | |
} else { | |
// We are already here | |
return; | |
} | |
} | |
defer(name, self._current, ScrollView.get(name)); | |
self._prev = self._current; | |
self._current = name; | |
self._deps.changed(); | |
}; | |
View.prototype.get = function() { | |
var self = this; | |
self._deps.depend(); | |
return self._current; | |
}; | |
View.prototype.setDefault = function(name) { | |
var self = this; | |
self._prev = name; | |
self._current = name; | |
}; | |
View.prototype.refresh = function() { | |
var self = this; | |
// Update iscroll | |
var scroll = ScrollView.get(self._current); | |
if (scroll) scroll.refresh(); | |
}; | |
contentsView = new View('main'); | |
} |
Logic for backbutton:
AppBackRoute = {
'foo': 'foos',
'foos': 'main'
};
cordovaApi.addEventListener('backbutton', function(e) {
var currentView = contentsView.get();
if (currentView === 'main') {
// If current page is main then exit the app
cordovaApi.close();
} else {
// Else goto main - or last page?
contentsView.set(AppBackRoute[currentView] || 'main');
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
contentsView.get() - returns the name of the current view
contentsView.set('id'); - activate div with id