Skip to content

Instantly share code, notes, and snippets.

@searls
Created December 8, 2011 19:16
Show Gist options
  • Save searls/1448126 to your computer and use it in GitHub Desktop.
Save searls/1448126 to your computer and use it in GitHub Desktop.
An annoyance when using eco and you pass it a backbone model, you must bind Model#escape yourself.
//This is a template generated by eco
var ecoTemplate = function (__obj) {
if (!__obj) __obj = {};
var __out = [], __capture = function(callback) {
var out = __out, result;
__out = [];
callback.call(this);
result = __out.join('');
__out = out;
return __safe(result);
}, __sanitize = function(value) {
if (value && value.ecoSafe) {
return value;
} else if (typeof value !== 'undefined' && value != null) {
return __escape(value); //<-- here's the offending line
} else {
return '';
}
}, __safe, __objSafe = __obj.safe, __escape = __obj.escape;
__safe = __obj.safe = function(value) {
if (value && value.ecoSafe) {
return value;
} else {
if (!(typeof value !== 'undefined' && value != null)) value = '';
var result = new String(value);
result.ecoSafe = true;
return result;
}
};
if (!__escape) {
__escape = __obj.escape = function(value) {
return ('' + value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
};
}
(function() {
__out.push('I has info: ');
__out.push(__sanitize(this.hasInfo()));
__out.push('\n');
}).call(__obj);
__obj.safe = __objSafe, __obj.escape = __escape;
return __out.join('');
};
//And here's a backbone model that has a method on it
var MyModel = Backbone.Model.extend({
hasInfo: function(){ return true; }
});
describe('eco calling __obj.escape()',function(){
context("without binding #escape", function(){
it("explodes",function(){
expect(function(){
ecoTemplate(new MyModel())
}).toThrow();
});
});
context("when binding #escape", function(){
var myModel;
beforeEach(function(){
myModel = new MyModel();
_.bindAll(myModel);
});
it("does *NOT* explode",function(){
expect(function(){
ecoTemplate(myModel)
}).not.toThrow();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment