This repository has been archived by the owner on Dec 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
75 lines (68 loc) · 1.83 KB
/
main.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
var Notatka = Backbone.Model.extend({
defaults: {
title: "new note"
}
});
var NotatkaCollection = Backbone.Firebase.Collection.extend({
model: Notatka,
url: "https://notatka-49184.firebaseio.com"
});
// Individual note item
var noteView = Backbone.View.extend({
tagName: "li",
template: _.template("<%= title %>"),
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
},
});
// The view for the entire application
var AppView = Backbone.View.extend({
el: $('#notatka'),
initialize: function() {
this.list = this.$("#note-items"); // the list to append to
this.listenTo(this.collection, 'add', this.addOne);
},
addOne: function(note) {
var view = new noteView({model: note});
this.list.append(view.render().el);
}
});
//submit by pressing enter
$('input').keyup(function(e){
if(e.keyCode == 13){
$(this).trigger('enter');
}
});
// The main view for the application
var AppView = Backbone.View.extend({
el: $('#notatka'),
events: {
"click #add-note" : "createNewNote",
"enter #add-note": "createNewNote",
},
initialize: function() {
this.list = this.$("#note-items");
// Input for new notes
this.input = this.$("#add-note");
this.listenTo(this.collection, 'add', this.addOne);
},
addOne: function(note) {
var view = new noteView({model: note});
this.list.append(view.render().el);
},
createNewNote: function(e) {
// Ensure input is not empty
if (!this.input.val()) { return; }
this.collection.create({title: this.input.val()});
this.input.val('');
}
});
function initFirebase() {
var collection = new NotatkaCollection();
var app = new AppView({ collection: collection });
}
$(function() { initFirebase() });