Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkbox still unchecked after triggering the event

Tags:

backbone.js

I have the following views:

   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));
            }
        }
    });

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 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?

like image 838
Dmitry Belaventsev Avatar asked Jan 29 '12 18:01

Dmitry Belaventsev


1 Answers

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.

like image 91
Dmitry Belaventsev Avatar answered Sep 30 '22 01:09

Dmitry Belaventsev