-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
102 lines (91 loc) · 2.95 KB
/
admin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
$(function() {
Parse.$ = jQuery;
// Replace this line with the one on your Quickstart Guide Page
Parse.initialize("bin8MzZfpkdxSQ5rq9L7Iq0gsCh95HgTNBuWwwW7", "R9ACofZwLvdvfizi3T1CbyWLSF0euUFC0X0uQ8RV");
var Blog = Parse.Object.extend('Blog', {
create: function(title, content) {
this.save({
'title': title,
'content': content,
'author': Parse.User.current(),
'authorName': Parse.User.current().get('username'),
'time': new Date().toDateString()
}, {
success: function(blog) {
alert('You added a new blog: ' + blog.get('title'));
},
error: function(blog, error) {
console.log(blog);
console.log(error);
}
});
}
});
var LoginView = Parse.View.extend({
template: Handlebars.compile($('#login-tpl').html()),
events: {
'submit .form-signin': 'login'
},
login: function(e) {
// Prevent Default Submit Event
e.preventDefault();
// Get data from the form and put them into variables
var data = $(e.target).serializeArray(),
username = data[0].value,
password = data[1].value;
// Call Parse Login function with those variables
Parse.User.logIn(username, password, {
// If the username and password matches
success: function(user) {
var welcomeView = new WelcomeView({ model: user });
welcomeView.render();
$('.main-container').html(welcomeView.el);
},
// If there is an error
error: function(user, error) {
console.log(error);
}
});
},
render: function(){
this.$el.html(this.template());
}
}),
WelcomeView = Parse.View.extend({
template: Handlebars.compile($('#welcome-tpl').html()),
events: {
'click .add-blog': 'add'
},
add: function(){
var addBlogView = new AddBlogView();
addBlogView.render();
$('.main-container').html(addBlogView.el);
},
render: function(){
var attributes = this.model.toJSON();
this.$el.html(this.template(attributes));
}
});
var loginView = new LoginView();
loginView.render();
$('.main-container').html(loginView.el);
var AddBlogView = Parse.View.extend({
template: Handlebars.compile($('#add-tpl').html()),
events: {
'submit .form-add': 'submit'
},
submit: function(e){
// Prevent Default Submit Event
e.preventDefault();
// Take the form and put it into a data object
var data = $(e.target).serializeArray(),
// Create a new instance of Blog
blog = new Blog();
// Call .create()
blog.create(data[0].value, data[1].value);
},
render: function(){
this.$el.html(this.template());
}
});
});