Created
May 19, 2013 19:27
-
-
Save marti1125/5608681 to your computer and use it in GitHub Desktop.
Backbone 04 codeschool model and view
This file contains hidden or 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
var AppointmentView = Backbone.View.extend({ | |
template: _.template('<span><%= title %></span><a href="#">x</a>'), | |
events: {"click a": "cancel"}, | |
cancel: function() { | |
this.model.set({cancelled:true}); | |
}, | |
render: function(){ | |
this.$el.html(this.template(this.model.toJSON())); | |
} | |
}); | |
//model | |
var Appointment = Backbone.Model.extend({ | |
cancel: function(){ | |
this.set({cancelled: true}); | |
this.save(); | |
} | |
}); | |
//view | |
var AppointmentView = Backbone.View.extend({ | |
template: _.template('<span><%= title %></span> <a href="#">x</a>'), | |
events: { "click a": "cancel" }, | |
cancel: function(){ | |
this.model.cancel(); | |
}, | |
render: function(){ | |
this.$el.html(this.template(this.model.toJSON())); | |
} | |
}); | |
//app | |
var AppointmentView = Backbone.View.extend({ | |
template: _.template('<span class="<% if(cancelled) print("cancelled") %>">' + | |
'<%= title %></span>' + | |
'<a href="#">x</a>'), | |
initialize: function(){ | |
this.model.on('change', this.render, this); | |
}, | |
events: { "click a": "cancel" }, | |
cancel: function(){ | |
this.model.cancel(); | |
}, | |
render: function(){ | |
this.$el.html(this.template(this.model.toJSON())); | |
} | |
}); | |
//remove - destroy | |
var AppointmentView = Backbone.View.extend({ | |
template: _.template('<span class="<% if(cancelled) print("cancelled") %>">' + | |
'<%= title %></span>' + | |
'<a href="#">x</a>'), | |
initialize: function(){ | |
this.model.on('change', this.render, this); | |
this.model.on('destroy', this.remove, this); | |
}, | |
events: { "click a": "cancel" }, | |
cancel: function(){ | |
this.model.cancel(); | |
}, | |
render: function(){ | |
this.$el.html(this.template(this.model.toJSON())); | |
}, | |
remove: function() { | |
this.$el.remove(); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment