Skip to content

Instantly share code, notes, and snippets.

@raix
Last active August 29, 2015 14:16
Show Gist options
  • Save raix/2473fb7d3fd071251ea2 to your computer and use it in GitHub Desktop.
Save raix/2473fb7d3fd071251ea2 to your computer and use it in GitHub Desktop.
A ultra simple view handler
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');
}
@raix
Copy link
Author

raix commented Feb 27, 2015

contentsView.get() - returns the name of the current view
contentsView.set('id'); - activate div with id

@raix
Copy link
Author

raix commented Feb 27, 2015

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