Skip to content

Instantly share code, notes, and snippets.

@shayne
Created October 14, 2010 11:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shayne/1ac113d3cc2fef9fbe99 to your computer and use it in GitHub Desktop.
Save shayne/1ac113d3cc2fef9fbe99 to your computer and use it in GitHub Desktop.
// This would be made available via Jammit
var Templates = {
"tasks/task": _.template("Task: <%= title %>")
}
var Task = Backbone.Model;
var TaskView = Backbone.View.extend({
tagName: "li",
initialize: function() {
_.bindAll(this, "render");
},
template: Templates["tasks/task"],
render: function() {
$(this.el).html(this.template(this.model.toJSON()));
this.handleEvents();
return this;
}
});
var TaskCollection = Backbone.Collection.extend({
model: Task
});
var Tasks = new TaskCollection;
function viewForTask(task) {
return new TaskView({
model: task,
id: "task-" + task.cid
});
}
Tasks.bind("add", function(task) {
var view = viewForTask(task).render();
$("body").append(view.el);
// still duplicate
task.bind("change", view.render);
});
Tasks.bind("refresh", function(tasks) {
var els = [];
tasks.each(function(task) {
var view = viewForTask(task).render();
els.push(view.el);
// still duplicate
task.bind("change", view.render);
});
$("body").append(els);
});
Tasks.refresh([
{title: "Task 1"},
{title: "Task 2"}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment