Skip to content

Instantly share code, notes, and snippets.

@iolo
Created September 11, 2012 08:36
Show Gist options
  • Save iolo/3696929 to your computer and use it in GitHub Desktop.
Save iolo/3696929 to your computer and use it in GitHub Desktop.
<script type="text/template" id="item-template">
<div class="view">
<input class="toggle" type="checkbox" <%= done ? 'checked="checked"' : '' %> />
<label><%- title %></label>
<a class="destroy"></a>
</div>
<input class="edit" type="text" value="<%= title %>" />
</script>
<script type="text/javascript">
var Todo = Backbone.Model.extend({
defaults: function() {
return {
title: "empty todo...",
order: Todos.nextOrder(),
done: false
};
},
initialize: function() {
if (!this.get("title")) {
this.set({"title": this.defaults().title});
}
}
});
var TodoView = Backbone.View.extend({
template: _.template($('#item-template').html()),
initialize: function() {
this.model.on('change', this.render, this);
this.model.on('destroy', this.remove, this);
},
render: function() {
this.$el.html(this.template(this.model.toJSON()));
this.$el.toggleClass('done', this.model.get('done'));
return this;
},
...
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment