gpt4 book ai didi

backbone.js - 触发事件后复选框仍未选中

转载 作者:行者123 更新时间:2023-12-04 19:16:23 24 4
gpt4 key购买 nike

我有以下观点:

   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 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”类的元素。此元素是复选框。我点击这个复选框。在两个 View 中调用 toBasket() 方法后,我仍然看到未选中的复选框。为什么会这样?

最佳答案

错误在于渲染。在为模型的属性渲染函数设置新值后,调用并“重绘”复选框(因此,这可能是主干的错误 - 重新渲染后,复选框的状态不会保存)。所以我在模板中添加了一条短线,如有必要,为复选框添加“已检查”属性。

关于backbone.js - 触发事件后复选框仍未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9055552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com