public
Created

don't route commands and events

  • Download Gist
1-routing-delete.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
SomeModel = Backbone.Model.extend({});
 
SomeCollection = Backbone.Collection.extend({
model: SomeModel
});
 
SomeView = Backbone.view.extend({
el: "#some-model",
template: "#some-template",
 
render: function(){
var html = $(this.template).tmpl(this.model);
$(this.el).html(html);
}
});
 
SomeRouter = Backbone.Router.extend({
routes: {
'#/delete/:id': 'delete'
},
 
delete: function(id){
model = someCollection.find(id);
model.destroy();
$("#some-model").remove();
}
});
2-routing-delete.html
HTML
1 2 3 4
<div id="some-model">
<p>display some data from the model, here</p>
<a class="delete" href="#/delete/1">delete</a>
</div>
3-view-delete.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
SomeModel = Backbone.Model.extend({});
 
SomeCollection = Backbone.Collection.extend({
model: SomeModel
});
 
SomeView = Backbone.view.extend({
el: "#some-model",
template: "#some-template",
 
events: {
"click a.delete": "delete"
},
 
render: function(){
var html = $(this.template).tmpl(this.model);
$(this.el).html(html);
},
 
delete: function(e){
e.preventDefault();
this.model.destroy();
this.remove();
}
});
4-view-delete.html
HTML
1 2 3 4
<div id="some-model">
<p>display some data from the model, here</p>
<a class="delete" href="#">delete</a>
</div>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.