Skip to content

Commit

Permalink
aesthetics
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed May 17, 2012
1 parent a645ddb commit f1a70fc
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions script/todolist.js
@@ -1,33 +1,34 @@

window.onload =function() {
//get browser coords
navigator.geolocation.getCurrentPosition(function(loc){window.geo ={'latitude':loc.coords.latitude,'longitude':loc.coords.longitude}});

var todo = Backbone.Model.extend({

var todo = Backbone.Model.extend({
defaults: {
id : "random",
content:"blank",
geolocation:{latitude:0,longitude:0},
done: false
},
done: false},

initialize:function(){
this.set({geolocation: window.geo});
},

toggle:function(){
this.set({done: !this.get("done")})
},

clear: function() {
this.destroy();
},
store:function(){
}
})


var todolist = Backbone.Collection.extend({
model:todo,

localStorage: new Backbone.LocalStorage("todolist"),
initialize:function(){

},

done: function() {
return this.filter(function(todo){ return todo.get('done'); });
},
Expand All @@ -42,46 +43,60 @@ window.onload =function() {
return todo.get('id');
}
})

//collection needs to be instantiated to be used by view
var Todolist = new todolist;

var geo;

var todoview = Backbone.View.extend({
tagName: 'li',

//template: _.template($('#item-template').html()),
events: {
"click .check" : "toggleDone"},
events: {"click .check" : "toggleDone"},

initialize: function(){
//view observers on the model
_.bindAll(this, 'render', 'close', 'clear');
this.model.on('destroy', this.clear);
},

render: function() {
var template= _.template($('#item-template').html());
this.$el.html(template(this.model.toJSON()));
//this.input = this.$('.todo-input');
return this
},

close: function() {
this.model.save({content: this.input.val()});
},

toggleDone: function() {
this.model.toggle();
},

edit : function(){
this.input.focus();
},

clear:function(){
this.remove();
this.unbind();
},
})


var Appview = Backbone.View.extend({
el:$("#todo-app"),

donetemplate: _.template($("#done-template").html()),

events:{
"keypress #todo-input" : "createOnEnter",
"click #plus": "createOnClick",
"click #clear-done": "clearDoneTasks"},

initialize:function(){
//Appview observers to the collection
_.bindAll(this, 'addOne','addAll','checkcompleted','clearDoneTasks');
Expand All @@ -92,54 +107,64 @@ window.onload =function() {
Todolist.on('change:done', this.checkcompleted);
Todolist.fetch();
},

createOnEnter:function(e){
if (e.keyCode != 13) return;
this.createtodo(this.$("#todo-input").val());
},

createOnClick:function(){
this.createtodo(this.$("#todo-input").val());
},

getstats:function(){
var id = Todolist.nextOrder();
var content = this.$("#todo-input").val();
done = false
return {id:id,geolocation:window.geo,content:content};
},

addOne: function(todo) {
var view = new todoview({model: todo});
this.$("#todo-list").append(view.render().el);
this.checkcompleted();
},

addAll:function(){
Todolist.each(this.addOne);
},

createtodo:function(content){
if (content==""){return};
Todolist.create({content:content,id:Todolist.nextOrder()});
this.$("#todo-input").val("");
},

checkcompleted:function(){
if (Todolist.done().length >0){
this.$("#done").html(this.donetemplate({done:Todolist.done().length}))}
else{this.$("#done").html("")}
},

clearDoneTasks:function(){
_.each(Todolist.done(), function(todo){ todo.clear();});
}
})

var App;



App = new Appview({el:$("#todo-app")});

//app router
var appRouter = Backbone.Router.extend({
routes: {"add/:query": "addTodo"},

addTodo:function(query){
App.createtodo(query);
}

})

var approutes = new appRouter;

Backbone.history.start()
};

0 comments on commit f1a70fc

Please sign in to comment.