Search code examples
jquery-uibackbone.js

How to bind backbone.js events to JQuery UI dialogue windows?


I'm very new to backbone.js but we're starting to use more and more JS on the front end and I wanted to use some framework to give the code structure - backbone seems to suit our needs.

So started off with a very simple test app that launches a dialogue window using jquery-ui. The problem I have is that since jquery-ui adds a wrapper DIV round the original template used by backbone, the events no longer fire.

I don't want to use the jquery-ui event model as I'd rather just use the one - how can I bind backbone's to this new structure?


Solution

  • OK - at the end of this project, I finally realised that I hadn't answered this. What happens is when you create a .dialog with JQueryUI, it actually detatches your original DOM element and moves it to the bottom of the DOM wrapped in it's own JQueryUI markup to turn it into a dialog.

    Since the Backbone view's element has now been moved, Backbone doesn't pick up any events that fire as it's happening outside it's own "view" as far as it is concerned.

    So what you have to do is reassign the view's element:

    var dlg = this.$("#dialogue-properties").dialog({ ..});
    this.setElement(dlg);
    

    Where this is the view. This is done within the initialize method