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