Guest User

Untitled

a guest
Dec 11th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. (function ($) {
  2. var answers = [];
  3. var app = $.sammy('#book', function () {
  4. //the type of template method we're using
  5. this.use('Template');
  6. //remember the selections for the final page
  7. //var store = new Sammy.Store;
  8. //get everything started
  9. this.around(function (callback) {
  10. var context = this;
  11. //get the data
  12. this.load('data/items.json').then(function (chapters) {
  13. context.items = chapters;
  14. }).then(callback);
  15. });
  16. //initial page view
  17. this.get('#/', function (context) {
  18. //add the correct class to the body
  19. $('body').attr({
  20. 'class': 'page ' + this.items.chapters[0]
  21. });
  22. this.partial('templates/chapter-1.template');
  23. });
  24. //do some sort of fancy transition between pages
  25. this.swap = function(content) {
  26. this.$element().hide().html(content).fadeIn('fast');
  27. }
  28. //page 5 is actually stage two of page four
  29. this.get('#/chapter/5/:answer', function (context) {
  30. //empty out the content from the page
  31. context.app.swap('');
  32. //store.set('four', this.params['answer']);
  33. answers[3] = this.params['answer'];
  34. $('body').attr({
  35. 'class': 'page four'
  36. });
  37. //render the page if you don't mind!
  38. this.partial('templates/chapter-4.template');
  39. //
  40. });
  41. //final page
  42. this.get('#/chapter/6/:answer', function (context) {
  43. //empty out the content from the page
  44. context.app.swap('');
  45. //store.set('five', this.params['answer']);
  46. answers[4] = this.params['answer'];
  47. $('body').attr({
  48. 'class': 'page five'
  49. });
  50. //render the page if you don't mind!
  51. this.partial('templates/chapter-5.template');
  52. });
  53. //all the rest
  54. this.get('#/chapter/:id/:answer', function (context) {
  55. //because arrays start from 0 and our pages start from 1 we subtract 2
  56. //store.set(this.items.chapters[this.params['id'] - 1], this.params['answer']);
  57. answers[this.params['id'] - 2] = this.params['answer'];
  58. //empty out the content from the page
  59. context.app.swap('');
  60. $('body').attr({
  61. 'class': 'page ' + this.items.chapters[this.params['id'] - 1]
  62. });
  63. //DRY, get the current page from the url
  64. var page = 'templates/chapter-' + this.params['id'] + '.template';
  65. //render the page if you don't mind!
  66. this.partial(page);
  67. //
  68. });
  69. //redirect to chapter 1 of the app if any other hash is accessed
  70. this.get('', function() { with(this) {
  71. redirect("#/");
  72. }});
  73. });
  74. $(function () {
  75. //go to the first page and initialse the framework
  76. app.run('#/');
  77. //load external sites in a new window if required
  78. $('a[rel=external]').attr('target', '_blank');
  79. });
  80. })(jQuery);
Add Comment
Please, Sign In to add comment