-
-
Save shayne/1ac113d3cc2fef9fbe99 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
// 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