Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function ($) {
- var answers = [];
- var app = $.sammy('#book', function () {
- //the type of template method we're using
- this.use('Template');
- //remember the selections for the final page
- //var store = new Sammy.Store;
- //get everything started
- this.around(function (callback) {
- var context = this;
- //get the data
- this.load('data/items.json').then(function (chapters) {
- context.items = chapters;
- }).then(callback);
- });
- //initial page view
- this.get('#/', function (context) {
- //add the correct class to the body
- $('body').attr({
- 'class': 'page ' + this.items.chapters[0]
- });
- this.partial('templates/chapter-1.template');
- });
- //do some sort of fancy transition between pages
- this.swap = function(content) {
- this.$element().hide().html(content).fadeIn('fast');
- }
- //page 5 is actually stage two of page four
- this.get('#/chapter/5/:answer', function (context) {
- //empty out the content from the page
- context.app.swap('');
- //store.set('four', this.params['answer']);
- answers[3] = this.params['answer'];
- $('body').attr({
- 'class': 'page four'
- });
- //render the page if you don't mind!
- this.partial('templates/chapter-4.template');
- //
- });
- //final page
- this.get('#/chapter/6/:answer', function (context) {
- //empty out the content from the page
- context.app.swap('');
- //store.set('five', this.params['answer']);
- answers[4] = this.params['answer'];
- $('body').attr({
- 'class': 'page five'
- });
- //render the page if you don't mind!
- this.partial('templates/chapter-5.template');
- });
- //all the rest
- this.get('#/chapter/:id/:answer', function (context) {
- //because arrays start from 0 and our pages start from 1 we subtract 2
- //store.set(this.items.chapters[this.params['id'] - 1], this.params['answer']);
- answers[this.params['id'] - 2] = this.params['answer'];
- //empty out the content from the page
- context.app.swap('');
- $('body').attr({
- 'class': 'page ' + this.items.chapters[this.params['id'] - 1]
- });
- //DRY, get the current page from the url
- var page = 'templates/chapter-' + this.params['id'] + '.template';
- //render the page if you don't mind!
- this.partial(page);
- //
- });
- //redirect to chapter 1 of the app if any other hash is accessed
- this.get('', function() { with(this) {
- redirect("#/");
- }});
- });
- $(function () {
- //go to the first page and initialse the framework
- app.run('#/');
- //load external sites in a new window if required
- $('a[rel=external]').attr('target', '_blank');
- });
- })(jQuery);
Add Comment
Please, Sign In to add comment