sstephenson/eco — Issue #36: Escape backbone model attributes causes error
Author: marlun
Created: 2012-01-22 11:05:31+00:00
State: open

I've been trying out using stitch.js with backbone.js but I'm having
some troubles.

I'm sending a backbone model to a Eco template with:

$(this.el).html(template(this.model));

Then in my template, all I'm trying is this:

<%= @get('text') %>

Now what I've been able to figure out is that stitch first checks if
there's already an escape function on the object in question and since
it's a backbone model there is a method called escape so stitch calls
that method instead of its own. The backbone escape method looks likes
this:

escape : function(attr) {
var html;
if (html = this._escapedAttributes[attr]) return html;
var val = this.attributes[attr];
return this._escapedAttributes[attr] = escapeHTML(val == null ? ''
: '' + val);
},

Now since when this is called this is DOMWindow the
this._escapedAttributes[attr] part fails with an error:

Uncaught TypeError: Cannot read property 'Finish some great tasks' of undefined
_.extend.escape
__sanitize
__obj.safe
__obj.safe
module.exports
module.exports.Backbone.View.extend.render
module.exports.Backbone.View.extend.add
_.each._.forEach
_.each.Backbone.Collection.(anonymous function)
module.exports.Backbone.View.extend.addAll
Backbone.Events.trigger
_.extend.reset
_.extend.fetch.options.success
jQuery.Callbacks.fire
jQuery.Callbacks.self.fireWith
done
jQuery.ajaxTransport.send.callback

'Finish some great tasks' is what's in my models 'text' attribute.

I tought maybe I could change __esacpe(value) in your code to
__escape.call(__obj, value) but I havn't got it working yet. Any
ideas?

Also I can change the template to <%- @escape('text') %> and it works
but I'm not sure what I think is best and I thought I would tell you
about this problem.