复选框触发事件后,仍然未选中 [英] Checkbox still unchecked after triggering the event
问题描述
我有以下几点看法:
window.DmnView = Backbone.View.extend({
template: _.template($("#tmpl_dmnListItem").html()),
events: {
"click .getWhois": "showWhois",
"click .getDomain": "toBasket"
},
initialize: function() {
this.model.bind('change', this.render, this);
this.model.bind('destroy', this.remove, this);
this.bind('toBasket', dmnListApp.toBasket, this);
},
render: function() {
return $(this.el)
.attr("class", this.model.get("free") ? "dmnItem green" : this.model.get("checked") ? "dmnItem red" : "dmnItem red loader")
.html(this.template(this.model.toJSON()));
},
remove: function() {
$(this.el).remove();
},
showWhois: function() {
showBoxes(this.model.get("info"));
return false;
},
toBasket: function() {
this.model.toBasket();
this.trigger('toBasket');
}
});
window.DmnListApp = Backbone.View.extend({
el: $("#regWrap"),
events: {
"keypress #dmnName": "checkAll"
},
initialize: function() {
this.input = this.$("#dmnName");
this.list = this.$("#dmnList");
this.basket = this.$("#dmnBasket");
dmnList.bind('add', this.addOne, this);
dmnList.bind('all', this.render, this);
},
render: function() {
},
addOne: function(dmnItem) {
var view = new DmnView({model : dmnItem});
this.list.append(view.render());
},
checkOne: function(name, zone, price, days) {
dmnList.create({name: name, zone: zone, price: price, days: days});
},
checkAll: function(e) {
var name = this.input.val();
if (!name || e.keyCode != 13) return;
if (name == "")
name = "yandex";
dmnList.destroyAll();
var zoneList = dmnList.domainsInfo.Name;
var costList = dmnList.domainsInfo.CostOrder;
var daysList = dmnList.domainsInfo.DaysToProlong;
var parent = this;
$.each(zoneList, function(key, zone) {
parent.checkOne(name, zone, costList[key], daysList[key]);
});
this.input.val("");
},
toBasket: function(){
if (this.model.get("inBasket")){
dmnListApp.basket.append($(this.el));
}else{
dmnListApp.list.append($(this.el));
}
}
});
和我有DmnItem查看下面的模板:
And I have the following template for DmnItem View:
<script id="tmpl_dmnListItem" type="text/template">
<%= checked&&free ? "<input type='checkbox' class='getDomain' />" : ""%><%= name %>.<%= zone %> <%= (free || !checked ) ? (checked) ? '<p class="fr">'+price+" руб./"+days+'</p>' : "" : "<a href='#' class='getWhois fr'>WhoIs</a>" %>
</script>
DmnView监听点击元素上与getDomain级。此元素的复选框。我点击此复选框。并调用toBasket()方法,在这两个意见后,我见犹选中复选框。为什么会发生这样呢?
DmnView listen for clicking on element with the "getDomain" class. This element is the checkbox. I click on this checkbox. And after calling toBasket() method in both Views I see still unchecked checkbox. Why it happened so?
推荐答案
该缺陷是渲染。设置新的价值模型属性渲染取景功能后,打来电话,重绘复选框(这样,它也许骨干错误 - 重新渲染之后,复选框的状态不保存)。所以我增加了一个简短线模板,里面加选中如果有必要的复选框属性。
The bug was in rendering. After setting new value to model's attribute render function of view was called and "redraw" the checkbox (so, it's maybe a bug of backbone - after re-Rendering, state of checkbox don't saved). So I added a short line to template, which add "checked" attribute for checkbox if necessary.
这篇关于复选框触发事件后,仍然未选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!