Skip to content

Instantly share code, notes, and snippets.

@jacobthemyth
Created February 18, 2015 21:07
Show Gist options
  • Save jacobthemyth/007580aaf21e90aa6f00 to your computer and use it in GitHub Desktop.
Save jacobthemyth/007580aaf21e90aa6f00 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app-container"></div>
<script type="text/template" id="index-template">
<h1>Todo</h1>
<ul class="task-list">
</ul>
</script>
<script type="text/template" id="task-item-template">
<input type="checkbox" <%= complete ? "checked" : ""%>>
<label for=""><%= title %></label>
</script>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/underscore/underscore.js"></script>
<script src="bower_components/backbone/backbone.js"></script>
<script src="main.js"></script>
</body>
</html>
(function(){
'use strict';
var Task = Backbone.Model.extend({
idAttribute: 'objectId',
defaults: {
title: '',
complete: false
}
});
var TasksCollection = Backbone.Collection.extend({
model: Task,
url: "https://api.parse.com/1/classes/Task",
parse: function(response){
return response.results;
}
});
var TaskIndexView = Backbone.View.extend({
tagName: 'ul',
template: _.template($('#index-template').text()),
initialize: function(){
this.listenTo(this.collection, 'sync', this.render);
},
render: function(){
this.$el.html(this.template());
this.renderChildren();
return this;
},
renderChildren: function(){
this.childViews = this.collection.map(function(task){
return new TaskItemView({model: task});
});
var self = this;
_.each(this.childViews, function(view){
view.render();
self.$('.task-list').append(view.el);
});
}
});
var TaskItemView = Backbone.View.extend({
tagName: 'li',
template: _.template($('#task-item-template').text()),
events: {
'change input': 'toggleComplete'
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
},
toggleComplete: function(){
var isChecked = this.$('input').prop('checked');
this.model.set('complete', isChecked);
this.model.save();
}
});
var AppRouter = Backbone.Router.extend({
routes: {
'': 'index'
},
initialize: function(){
this.tasks = new TasksCollection();
this.indexView = new TaskIndexView({
el: '#app-container',
collection: this.tasks
});
},
index: function(){
this.tasks.fetch();
this.indexView.render();
}
});
$.ajaxSetup({
headers: {
"X-Parse-Application-Id": "ZYQHOvrj5oPV8fGAN6M7x6m00ZNLtr5tpd3gzkFi",
"X-Parse-REST-API-Key": "0BRysAUoHF2WsbkYZh1DOosJS3Oi1uS31KozIKUz"
}
});
$(document).ready(function(){
window.router = new AppRouter();
Backbone.history.start();
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment