編輯:忘了提醒我想起了如下設置templateSettings讀者:無法獲取變量在underscore.js模板插值而jasmine.js測試
_.templateSettings = {
interpolate : /\{\{([\s\S]+?)\}\}/g
};
我有一個很難得到一個varialbe插入下劃線,同時運行我的Jasmine規格。鑑於模板,繪製方法,並在下面茉莉測試,我能夠得到模板正確通過插值變量:
_.template(
boneHeaderInstance.template.html(),
{ id:boneHeaderInstance.id,
columns:boneHeaderInstance.columns
}
)
雖然這不能插變量列:
boneHeader = Backbone.View.extend({
el: $('#boneTableHeader'),
template: $('#boneTableHeaderTemplate'),
initialize: function(){
this.id = 'boneTableHeader';
this.el = $('#' + this.id);
this.columns = 'blah';
this.template = $('#' + this.id + 'Template');
this.render();
return this;
},
render: function(){
var that = this;
var data = {id: that.id, columns: that.columns}
this.el.html(_.template(this.template.html(), data));
}
});
模板:
<script type = 'text/template' id = 'boneTableHeaderTemplate'>
<tr id = "{{obj.id}}Row">
{{obj.columns}}
</tr>
</script>
在Render方法:
render: function(){
var that = this;
var data = {id: that.id, columns: that.columns}
this.el.html(_.template(that.template.html(), data));
}
茉莉花測試:
describe('boneHeader', function(){
beforeEach(function(){
boneHeaderInstance = boneTableInstance.header;
});
describe('rendering', function(){
it('should have expected html', function(){
expect(
boneHeaderInstance.el.html().replace(/\s\t\n/ , '', 'g')
).toEqual(
_.template(boneHeaderInstance.template.html(),
{ id:boneHeaderInstance.id,
columns:boneHeaderInstance.columns
}).replace(/\s\t\n/ , '', 'g')
);
});
});
});
茉莉花結果:
Expected ' <tr id="boneTableHeaderRow"></tr> ' to equal ' <tr id = "boneTableHeaderRow"> blah </tr> '
謝謝。我以爲我已經包含了'templateSettings'。我正在使用它,並會更新我的問題以反映這一點。對困惑感到抱歉。現在回顧其餘的答案。 – kikuchiyo 2012-04-03 03:11:52
我仍在消化其餘的答案。我同意不應該需要'obj'。你知道爲什麼'obj.id'和'obj.columns'在此代碼'_.template( boneHeaderInstance.template.html(){ ID:boneHeaderInstance.id, 列:boneHeaderInstance.columns } )'與我的答案中提供的模板?如果沒有,不要大驚小怪,只是想知道。另外,我不認爲這是一個問題。考慮到它正確地插了'每茉莉花消息obj.id':'預期 '
這意味着你可能是正確的,將'{{obj.columns}}'直接放在'
相關問題