Sean Quinn
Sean Quinn

Reputation: 2171

Is it possible to return the Backbone View associated with an element ID?

Given the id of an element, is it possible to return a Backbone View (or any Backbone object for that matter) that has been associated with that element?

A good example of what I'm looking for would be something from the world of Dojo Toolkit, where: registry.byId('my-element-id') will return an associated widget by its ID (see: http://dojotoolkit.org/reference-guide/1.9/dijit/registry.html).

Is there anything comparable in Backbone?

Upvotes: 0

Views: 41

Answers (1)

Karolis
Karolis

Reputation: 2959

No. Unless you keep track of it yourself. For example if in your BaseView you would do something like

var BaseView = Backbone.View.extend({
  constructor: function () {
    Backbone.View.prototype.constructor.apply(this, arguments);
    this.$el.data('backbone-view', this);
  }
});

You could then retrieve the backbone view associated with any element like so

$("#my-element-id").data('backbone-view');

Upvotes: 1

Related Questions