sashomaga

sammy.js

Nov 28th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     <script>
  2.         /// Sammy.js
  3.         /// SPA
  4.  
  5.         /// routing (dinamic change of some content not page)
  6.         //'domain.com#/' - root
  7.         // bind function to url
  8.  
  9.  
  10.         // use browser history api (user experience back prev btns ...)
  11.  
  12.         var app = $.sammy('#main-content', function () {
  13.  
  14.             //routing (method, url , callback)
  15.             this.get('#/', function () { });
  16.  
  17.             this.get('#/about', function () { });
  18.  
  19.             this.get('#/item/:id', function (id) { }); // this.params['id'] <- object with all params
  20.             //main method (entry point)
  21.         });
  22.         app.run('#/');
  23.  
  24.         ///
  25.     </script>
  26.     <script>
  27.         (function () {
  28.             require.config({
  29.                 paths: {
  30.                     jquery: "libs/jquery-2.0.3",
  31.                     mustache: "libs/mustache",
  32.                     sammy: "libs/sammy-0.7.4",
  33.                     rsvp: "libs/rsvp.min",
  34.                     "http-requester": "libs/http-requester"
  35.                 }
  36.             })
  37.  
  38.             require(["jquery", "sammy", "mustache", "rsvp", "http-requester"],
  39.                             function ($, sammy, mustache, rsvp, request) {
  40.                                 var app = sammy("#main-content", function () {
  41.                                     this.get("#/", function () {
  42.                                         $("#main-content").html("View the students page");
  43.                                     });
  44.  
  45.                                     this.get("#/about", function () {
  46.                                         $("#main-content").html("View the students page");
  47.                                     });
  48.  
  49.                                     this.get("#/students", function () {
  50.                                         request.getJSON("api/students")
  51.                                             .then(function (data) {
  52.                                                 var studentsList = $("<ul />").addClass("students-list");
  53.                                                 var templateString = $("#student-template").html();
  54.                                                 var template = mustache.compile(templateString);
  55.                                                 for (var i in data) {
  56.                                                     var student = data[i];
  57.                                                     var templatedStudent = template(student);
  58.                                                     var studentItem =
  59.                                                         $("<li />")
  60.                                                             .addClass("student-item")
  61.                                                                 .html(templatedStudent);
  62.                                                     studentsList.append(studentItem);
  63.                                                 }
  64.                                                 $("#main-content").html(studentsList);
  65.                                             });
  66.                                     });
  67.  
  68.                                     this.get("#/student/:id", function () {
  69.                                         request.getJSON("api/students/" + this.params["id"] + "/marks")
  70.                                             .then(function (marks) {
  71.                                                 var marksList = $('<ul/>').addClass("marks-list");
  72.                                                 var templateString = $("#mark-template").html();
  73.                                                 var template = mustache.compile(templateString);
  74.                                                 for (var i in marks) {
  75.                                                     var mark = marks[i];
  76.                                                     var templatedMark = template(mark);
  77.                                                     //marksList.append(templatedMark);
  78.                                                     var markItem =
  79.                                                         $("<li />")
  80.                                                             .addClass("mark-item")
  81.                                                                 .html(templatedMark);
  82.                                                     marksList.append(markItem);
  83.                                                 }
  84.                                                 $("#main-content").html(marksList);
  85.                                             });
  86.                                     });
  87.  
  88.  
  89.                                 });
  90.  
  91.                                 app.run("#/");
  92.  
  93.                                 $("header").on("click", "nav ul li", function () {
  94.                                     $(this).siblings().removeClass("selected");
  95.                                     $(this).addClass("selected");
  96.                                 });
  97.                             });
  98.         }());
  99.     </script>
  100.     <nav>
  101.                 <ul>
  102.                     <li class="selected">
  103.                         <a href="#/">Home</a>
  104.                     </li>
  105.                     <li>
  106.                         <a href="#/students">Students</a>
  107.                     </li>
  108.                     <li>
  109.                         <a href="#/about">About</a>
  110.                     </li>
  111.                 </ul>
  112.             </nav>
  113.         </header>
  114.         <div id="main-content">
  115.         </div>
  116.  
  117.         <script id="student-template" type="x-mustache-template">
  118.             <a href="#/student/{{id}}">{{name}}</a>
  119.         </script>
  120.  
  121.         <script id="mark-template" type="x-mustache-template">
  122.             <p>
  123.                 {{subject}}: {{score}}
  124.             </p>
  125.         </script>
Advertisement
Add Comment
Please, Sign In to add comment