0

I've almost tried everything and even exactly copied the example: [in here][1]

However I can't get to see it work it. I've tried multiple alternations for the event binding but none of them seem to work. What can be the possible reason?

Div container;

Load

My event binding trials;

el: '#container',
        events: {
            "click button": "render"
        }

Also;

el: '#container',
    events: {
        "click button#add": "render"
    }

And finally this;

el: '#container',
events: {
            "click input[type=button]": "doSearch"  
        }

Is there any possibility of that I'm doing this wrong. Besides if I get it to work, would it be possible to assign click event for each function in the div, giving their id's instead of a more general "click button": "render"?

NOTE: I couldn't get the html code wrapped in code tag.

1
  • 1
    I prefer to use jquery on function for the elements which are dynamically are been added to the DOM after the view is rendered. For example initialize: function() { this.$el.on('click', 'button', handler); } Commented May 18, 2012 at 18:51

2 Answers 2

2

Is your input button a child element of container? Backbone won't find elements outside the scope of the view's element.

You can do "click #add" provided #add is a child element of #container.

Sign up to request clarification or add additional context in comments.

2 Comments

I've later set 'el:body' for a more general approach but still didn't work
Can you post a jsfiddle example with a little more code to see what you're doing?
1

First of all, I'll assume your code is from the Backbone view, correct?

Does the #container div exist in the DOM? I notice your examples are calling render on click, but usually render is where people put code to actually render the HTML templates.

The delegateEvents documentation on BackboneJS uses the following render call as their example:

 render: function() {
    $(this.el).html(this.template(this.model.toJSON()));
    return this;
  },

Note that until this fires, the example would technically not have anything for you to click on.

A quick way to test this would be to run this in the console after your application fires up:

$("#container")

If this returns an empty "[ ]", then you know you have problems. The second thing to consider is if it is actually clickable (because maybe it exists, but it's not clickable due to its position or size).

Therefor, the final test would be to run this in the console and try clicking on the div:

$("#container").click(function() { alert('clicked');});

2 Comments

Would it be possible to solve this problem without integrating jquery. I mean simply defining events in 'events:{}' would not do good?
My main point was for the OP to check if the implementation is correct (that there is a DOM element with the ID #container rendering). His examples indicated that no element of that ID may be in the DOM. The code I subsequently posted was just a suggestion on how to troubleshoot. The render code I posted was taken directly from the BackboneJS documentation. The OP could use any other framework or code sample to create the HTML. BackboneJS does not handle creating HTML unless you manually tell it to do so (as per example). Hope this clarifies things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.