Question

I have a custom jQuery widget that is being called from my app that uses backbone.js for MVC. How can I use the backbone events pattern in my custom jQuery widget?

function( $ ) {
  $.widget( "medex.chooser", {
  ...
  _create: function() {
      // Would I create a new backbone view here???
  }
}

Thanks.

Was it helpful?

Solution

I think you need to define a custom View that is then called by your custom widget.

In your app initialization code, for example:

APP = {};          // your app's global object
APP.Views = {};

APP.Views.WidgetView = Backbone.View.extend(
{
    events: {
        "click .grid1" : "onGrid1Click"
    },

    initialize: function() {
        // code here
    },

    onGrid1Click : function(evt) {
        // code here
    }
});

Then this will be the constructor code for your widget:

function Widget(element) {
   this.view = new APP.Views.WidgetView({ el: element });
}

This code might not be entirely valid, but should give you an idea of the structure you were looking for. Lemme know if this works for you.

OTHER TIPS

If you're still looking for an answer, you could convert a widget into a view. The basic rules for converting from a Widget to a View are these:

  • _create becomes initialize
  • this.element becomes this.$el
  • refresh becomes render
  • destroy becomes remove
  • $('.someDiv').myWidget() becomes new MyView({ el: $('.someDiv') });
  • options have to become constructor parameters, but can be handled a couple of different ways
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top