Search code examples
jqueryhtmlbackbone.jsbackbone-events

Backbone Js - not able to trigger button click events


I am learning backbone js and in that I was trying to write one sample application where I was trying to add data in list from text box.

Code is available at JSFiddle link - http://jsfiddle.net/ynkJE/2662/

Code for referance:

<!DOCTYPE html>
<html>  
<head>
<title></title>
</head>
<body>
<!--html-->
<div id="mainDiv">
    <div id="headerDiv">
        <input id="txtName" type="text" />
        <input id="btnEnter" type="button" value="Enter" />
    </div>
    <div id="gridDiv">
        <ul id="grdlist">
        </ul>
    </div>
</div>
<!--templates-->
<script type="text/template" id="grid-template">
    <%_.each(items, function(item){ %>
    <li>
        <label id="lblName"><%=item.name %>></label>
        <input type="button" id="btnEdit" value="Edit">
        <input type="button" id="btnDelete" value="Delete">
    </li>
    <%}); %>
</script>
<script type="text/template" id="item-template">
<li>
        <label id="lblName"><%= name %></label>
        <input type="submit" id="btnEdit" value="Edit">
        <input type="button" id="btnDelete" value="Delete">
</li>
</script>
<!--Javascript-->
<script src="JsLibs/jquery-2.1.1.js" type="text/javascript"></script>
<!--    <script src="JsLibs/json2.js" type="text/javascript"></script>-->
<script src="JsLibs/underscore.js" type="text/javascript"></script>
<script src="JsLibs/backbone.js" type="text/javascript"></script>
<script type="text/javascript">
    var App = {};

    App.NameModel = Backbone.Model.extend({
        defaults: {
            name: 'Ravi'
        }
    });

    App.NameList = Backbone.Collection.extend({
        model: App.NameModel
    });

    App.ListView = Backbone.View.extend({
        //tagName: 'li',
        el: '#gridDiv',
        initialize: function () {
            _.bindAll(this, 'render','editName');
        },
        render: function () {
            var template1 = $("#item-template").html();
            console.log(this.model.toJSON());
            this.$el.append(_.template(template1, this.model.toJSON()));
            return this;
        },
        events: {
            'click btnEdit': 'editName',
            'click btnDelete': 'deleteName'
        },
        editName: function (e) {
            //e.preventDefault();
            alert('edit');
        },
        deleteName: function () {
            alert('delete');
        }
    });

    var enterView = Backbone.View.extend({
        el: 'headerDiv'
    });

    $(document).ready(function () {
        $('#btnEnter').click(function () {
            var myModel = new App.NameModel({ name: $('#txtName').val() });
            var myView = new App.ListView({ model: myModel });
            //$('#gridDiv').append(myView.render().el);
            myView.render();
        });
    });


</script>
</body>
</html>

Solution

  • The selectors are wrong, you are missing # for the ID selectors:

    events: {
        'click #btnEdit': 'editName',
        'click #btnDelete': 'deleteName'
    },
    

    Also note that IDs must be unique, you should use classes instead.