如何使用 backbone.js 添加带有标签名称 age 的文本框并在模板中查看它?
<label> Age</label>
<input type = "text" name = "age" value="12"/>
我希望将其作为属性添加到模型中并在模板中查看。谁能帮忙?我了解 backbone.js 的基础知识。
最佳答案
不确定您想要什么,但这是基本示例:
var App = {};
App.Person = Backbone.Model.extend({});
App.View = Backbone.View.extend({
el: "#form",
render: function() {
var html = _.template($('#form-tpl').html(), this.model.toJSON());
this.$el.html(html);
}
});
$(function() {
var person = new App.Person({
name: 'Thomas',
age: 37
}),
app = new App.View({model: person});
app.render();
});
HTML:
<script type="text/template" id="form-tpl">
<label>Age:</label>
<input type="text" name="age" value="<%= age %>">
</script>
<div id="form"></div>
还有大量教程可用。祝你好运!
关于javascript - Backbone.js 模板示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15239755/