使用Handlebars绑定的主干UI模型
Backbone UI Model binding with Handlebars
我希望在使用手柄的Backbone应用程序中实现UI绑定。我举了一个简单的例子来演示设计模式,但我注意到它不会很好地扩展,因为每次更改模型值时都需要渲染整个模板。
有没有一种方法可以更新手把模板中的单个值,而不需要重新渲染整个模板?
HTML:
<script id="entry-template" type="text/x-handlebars-template">
<div class="box {{#if active}} active {{/if}}">
<h1>{{title}}</h1>
<p>{{description}}</p>
<button class="btn" data-class="toggleActive">Toggle Active</button>
</div>
</script>
JS:
var TestModel = Backbone.Model.extend({
defaults: {
title: "Test",
description: "This is a test description!",
active: false
}
});
var TestView = Backbone.View.extend({
template: Handlebars.compile($("#entry-template").html()),
events: {
"click [data-class='toggleActive']": "toggleActive"
},
initialize: function(){
this.model = new TestModel();
this.render();
this.model.on("change", this.renderUI, this);
},
render: function(){
var self = this;
$('body').html(self.$el.html(self.template(self.model.toJSON())));
},
renderUI: function(){
this.$el.html(this.template(this.model.toJSON()));
},
toggleActive: function(){
var isActive = this.model.get("active");
this.model.set("active", !isActive);
}
});
var testView = new TestView();
如果您的视图知道模板中使用元素的每个地方的选择器,您可以执行以下操作,为每个属性更改创建一个单独的处理程序,并使用您提供的选择器更新相关的HTML
var TestView = Backbone.View.extend({
template: Handlebars.compile($("#entry-template").html()),
events: {
"click [data-class='toggleActive']": "toggleActive"
},
modelBindings: {
// property name: CSS selector of where to update
title: 'h1',
description: 'p'
},
initialize: function(){
this.model = new TestModel();
this.render();
var me = this;
function createHandler(selector) {
return function(model, value) {
me.$(selector).html(value);
}
}
for (var modelProp in this.modelBindings) {
this.model.on("change:" + modelProp, createHandler(this.modelBindings[modelProp]));
}
},
render: function(){
var self = this;
$('body').html(self.$el.html(self.template(self.model.toJSON())));
},
toggleActive: function(){
var isActive = this.model.get("active");
this.model.set("active", !isActive);
}
});
这只适用于您想要更新HTML的地方,而在您想要添加类的情况下则不起作用。在这种情况下,您可以使用相同的概念来添加/删除元素的属性
您可以监听模型上的特定属性更改,只更新相应的html元素。
侦听模型标题更改(this.listenTo
与this.on
相同,但它允许侦听器在调用this.stopListening
时解除绑定)。
this.listenTo(this.model, 'change:title', this.updateTitle);
然后创建updateTitle
处理程序。
updateTitle: function(model, value, options)
{
// this.$ === this.$el.find
this.$('h1').text(value);
}
相关文章:
- 使用导航属性创建Kendo UI网格模型的问题
- 将模型从Kendo Mvc UI网格传递到javascript函数
- Angularjs:如何让ui选择只有一种方式的模型竞标
- 初始化ng模型时,Angular ui选择占位符不起作用
- 使用Backbone在模型更改时渲染表单会导致表单UI错误
- 可以将模型传播到组件,但不能传播到sap.ui.jsfragment
- 如何将值与$scope绑定,以从定义状态ui路由器的控制器中使用ng模型进行查看
- Kendo UI自定义验证不适用于模板和视图模型
- UI Select在删除后不会更新模型
- 正在加载ui路由器状态下的共享模型
- 使用jQuery更新的Kendo UI网格值不会更新传递回来的模型
- 当多个对象共享同一视图模型时,如何在 kendo UI 中获取事件源
- 当可观察数组是其他模型的属性时,挖空不会更新 UI
- 在挖空中更新视图模型并更新 UI
- UI JavaScript在MVVM模型中属于什么位置(带有挖空)
- AngularJS ng模型形式由ng驱动,通过UI模型描述数据如何重复
- 使用Handlebars绑定的主干UI模型
- 如何在剑道UI模型定义中设置数字的小数点
- 从非剑道UI小部件上拖放剑道UI模型
- 剑道UI模型验证只在本地环境下工作