Opt*_*ino 5 javascript backbone.js underscore.js underscore.js-templating
我试图使用下划线模板呈现基本主干视图,但在尝试呈现模板时,我一直收到以下错误.
未捕获的ReferenceError:未定义金额
这里是jsfiddle:http://jsfiddle.net/rkj6j36n/
HTML
<body>
<div class="msg-con"></div>
</body>
Run Code Online (Sandbox Code Playgroud)
JS
DumbViewObj = Backbone.View.extend({
el: $('.msg-con'),
initialize:function(){
this.render();
},
render: function(){
var template = _.template('I am <%= amount %> dumb',{amount:'200'});
this.$el.append(template);
},
});
var dumb = new DumbViewObj();
Run Code Online (Sandbox Code Playgroud)
我确信解决方案很简单,但我无法弄清楚
epa*_*llo 16
因为template是一个函数而模板(obj)返回你所追求的字符串,所以在调用它之后它不会返回字符串.
你的代码在做什么
var xxx = template();
this.$el.append(xxx);
Run Code Online (Sandbox Code Playgroud)
你应该做什么
render: function(){
var template = _.template($('#dumb').html());
var vars = {amount:200};
var html = template(vars);
this.$el.append(html);
},
Run Code Online (Sandbox Code Playgroud)
在一行:
this.$el.append(_.template('I am <%= amount %> dumb')({amount:200}))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9319 次 |
| 最近记录: |