Advertisement
Guest User

Untitled

a guest
Mar 24th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var API_URL = 'http://localhost/octopus-phpfog/api';
  2. //var API_URL = 'http://octopus.phpfogapp.com/api';  
  3.  
  4. function Event(id, name, desc, dtStart, dtEnd) {
  5.     var self = this;
  6.     self.id = ko.observable(id);
  7.     self.name = ko.observable(name);
  8.     self.desc = ko.observable(desc);
  9.     self.dtStart = ko.observable(dtStart);
  10.     self.dtEnd = ko.observable(dtEnd);
  11.     self.registered = ko.observable(false);
  12. }
  13.  
  14. function EventsViewModel() {
  15.     var self = this;
  16.     self.events = ko.observableArray([]);
  17.     self.eventsCache = [];
  18.     self.selectedEvent = ko.observable(null);
  19.     self.searchQuery = ko.observable("");
  20.    
  21.     self.viewEvent = function(event) {
  22.         self.selectedEvent(event);
  23.         location.hash = "#" + event.id();
  24.     };
  25.    
  26.     self.loadUpcoming = function() {
  27.         // deep linking
  28.         var id = location.hash.replace("#", "");
  29.        
  30.         $.getJSON(API_URL + '/events/upcoming', function(events) {
  31.             $.each(events, function(i, event) {
  32.                 var evt = new Event(event.id, event.name, event.description, event.dtStart, event.dtEnd);
  33.                 self.events.push(evt);
  34.                 self.eventsCache.push(evt);
  35.                
  36.                 if (evt.id() == id)
  37.                     self.selectedEvent(evt);
  38.                 // check if event is registered already
  39.                 if ($.storage.get("reg" + event.id) == true)
  40.                     evt.registered(true)
  41.             });
  42.         });
  43.     };
  44.    
  45.     self.registrationValidationOpts = {
  46.         rules: {
  47.             name: {
  48.                 required: true,
  49.                 maxlength: 64
  50.             },
  51.             email: {
  52.                 required: true,
  53.                 maxlength: 128,
  54.                 email: true
  55.             },
  56.             contact: {
  57.                 required: true,
  58.                 digits: true,
  59.                 minlength: 8,
  60.                 maxlength: 8
  61.             },
  62.             faculty: {
  63.                 maxlength: 64
  64.             },
  65.             course: {
  66.                 maxlength: 64
  67.             },
  68.             year: {
  69.                 maxlength: 1,
  70.                 digits: true
  71.             }
  72.         },
  73.         messages: {
  74.             name: {
  75.                 required: "Please enter your name",
  76.                 maxlength: "Your name should not be more than {0} characters"
  77.             },
  78.             email: {
  79.                 required: "Please enter your email",
  80.                 maxlength: "Your email should not be more than {0} characters",
  81.                 email: "This email address appears to be invalid"
  82.             },
  83.             contact: {
  84.                 required: "Please enter your contact number",
  85.                 maxlength: "Your contact should be 8 digits long",
  86.                 minlength: "Your contact should be 8 digits long",
  87.                 digits: "Your contact should only contain digits"
  88.             },
  89.             faculty: {
  90.                 maxlength: "Your faculty should not be more than {0} characters"
  91.             },
  92.             course: {
  93.                 maxlength: "Your course should not be more than {0} characters"
  94.             },
  95.             year: {
  96.                 maxlength: "Your year of study should not be more than {0} digits long",
  97.                 digits: "Year of study should only contain digits"
  98.             }
  99.         }
  100.     };
  101.    
  102.     self.submitRegistration = function() {
  103.         var $frm = $("#frmRegister"),
  104.         formData = $.parseJSON($.toJSON($frm.serializeObject()));
  105.        
  106.         // validation  
  107.         $frm.validate(self.registrationValidationOpts);
  108.         if (!$frm.valid())
  109.             return false;
  110.        
  111.         // store particulars in local storage
  112.         $.storage.set("particulars", formData);
  113.         populateRegistrationForm();
  114.        
  115.         // submit request to server
  116.         $.post(API_URL + '/events/register/' + self.selectedEvent().id(),
  117.             formData,
  118.             function(json) {
  119.                 if (json.status == "success") {
  120.                     // mark event as registered
  121.                     self.selectedEvent().registered(true);
  122.                     $.storage.set("reg" + self.selectedEvent().id(), true); // store in local storage too
  123.                 } else {
  124.                     alert('Your registration was unsuccessful');
  125.                 }
  126.             });
  127.                
  128.         return false;
  129.     };
  130.    
  131.     self.search = function() {
  132.         var q = $.trim(self.searchQuery()), i, len;
  133.        
  134.         if (q === "") {
  135.             self.events.removeAll();
  136.             for (i = 0, len = self.eventsCache.length; i < len; i++) {
  137.                 self.events.push(self.eventsCache[i]);
  138.             }
  139.         } else {
  140.             self.events.removeAll();
  141.             for (i = 0, len = self.eventsCache.length; i < len; i++) {
  142.                 if (self.eventsCache[i].name().toLowerCase().indexOf(q) >= 0) {
  143.                     self.events.push(self.eventsCache[i]);
  144.                 }
  145.             }
  146.         }
  147.     };
  148.    
  149.     self.populateRegistrationForm = function() {
  150.         var user = $.storage.get("particulars");
  151.    
  152.         if (user) {
  153.             console.log($("#frmRegister"));
  154.             $("#frmRegister").find("input[name=name]").val(user.name).end()
  155.             .find("input[name=email]").val(user.email).end()
  156.             .find("input[name=contact]").val(user.contact).end()
  157.             .find("input[name=faculty]").val(user.faculty).end()
  158.             .find("input[name=course]").val(user.course).end()
  159.             .find("input[name=year]").val(user.year).end();
  160.         }
  161.         console.log("out")
  162.     }
  163.    
  164. }
  165.  
  166. $.fn.serializeObject = function() {
  167.     var o = {};
  168.     var a = this.serializeArray();
  169.     $.each(a, function() {
  170.         if (o[this.name]) {
  171.             if (!o[this.name].push) {
  172.                 o[this.name] = [o[this.name]];
  173.             }
  174.             o[this.name].push(this.value || '');
  175.         } else {
  176.             o[this.name] = this.value || '';
  177.         }
  178.     });
  179.     return o;
  180. };
  181.  
  182. $(document).ready(function() {
  183.     var eventsViewModel = new EventsViewModel();
  184.     ko.applyBindings(eventsViewModel);
  185.     eventsViewModel.loadUpcoming();
  186.     eventsViewModel.searchQuery.subscribe(eventsViewModel.search);
  187.    
  188.     // init local storage (jQuery Plugin)
  189.     $.storage = new $.store();
  190.    
  191.     // populate registration form with past data for convinence
  192.     console.log($("#frmRegister"))
  193.     eventsViewModel.populateRegistrationForm();
  194. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement