I found a good article on when to use the backbone escape
function. The author asserts that you should always use escape, apart from when you are definitely not going to be executing the value of a model attribute. For example if you were checking a model attribute was not null
:
var model = new Backbone.Model({foo: "Bar"});
if (model.get("foo") != null) { //notice how here we did not use escape
$("h1").html(model.escape("foo")); //but here we do
}
One related point to be aware of is that if you check for the returned value from model.escape("foo")
it will always return a string. So if you are expecting null
then you may be confused.
console.log(model.get("foo")); // null
console.log(model.escape("foo")); // ""
However, as Jeremy Ashkenas points out in a pull report querying this issue, it does not make sense to check the existence of an attribute after escaping it.