Created
February 5, 2016 15:11
-
-
Save SpencerCooley/ff0c50a416239fbe814a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Todo = require('../models/todo.js') | |
var TodoList = require('../collections/todolist.js') | |
var TodoView = Backbone.View.extend({ | |
el: '#todo-container', | |
events: { | |
'keyup .new-todo-box': 'handleEnter' | |
}, | |
handleEnter: function(e){ | |
if(e.keyCode == 13){ | |
this.createNewTodo(); | |
} | |
}, | |
initialize: function(){ | |
//TodoList collection is a dependency so we require it at the top of this file. | |
this.collection = new TodoList(); | |
this.listenTo(this.collection, "add", this.refreshList, this); | |
}, | |
refreshList: function(){ | |
var source = $("#list-item").html(); | |
var template = Handlebars.compile(source); | |
$('.todo-list').html(template(this.collection.toJSON())) | |
}, | |
createNewTodo: function(){ | |
//Todo model is a dependency so we require it at the top of this file. | |
var newTodo = new Todo(); | |
var message = $('.new-todo-box').val() | |
newTodo.set({'message': message }) | |
this.collection.add(newTodo) | |
$('.new-todo-box').val("") | |
} | |
}); | |
//simply just export your variable | |
//this will allow you to require it when creating other modules. | |
module.exports = TodoView; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment