1

Say I have a Backbone view and I'm regularly replacing the content of el, how would I re-run non-event actions on the new adjusted el contents? For example; say I've replaced the contents and want to have all links use a lightbox script. To attach the script normally, you'd do something like this in render;

this.$el.find('a').box()

But after the content replacement, that would need to be run again. Given that you don't necessarily know when the el contents are being replaced, how would you re-run that? I can't seem to find an event I can delegate to for that--ready, load and change all sound like they could work, but none do. Ideally, I'd like something that I can put in the events hash to call a method which runs those things after each replacement.

Stephen Belanger
  • 6,251
  • 11
  • 45
  • 49
  • Does the code that does the replacing always come from the same place? – Eli Mar 07 '12 at 00:18
  • There are several different components capable of replacing the content, so I need to figure out a sufficiently decoupled method of trigger some sort of "change" event to tell it to rerun any plugins it uses. – Stephen Belanger Mar 07 '12 at 00:26
  • Is the content itself a view, or part of a view? – Eli Mar 07 '12 at 00:37
  • 2
    Kind of the point of the backbone view I thought was to encapsulate this very concept - if you want to change the content of the el then you should be doing it within the view and calling render() when you're done to re-render the widget. Otherwise what's the point of encapsulation on the view if you're changing the contents of the el in other places? – tkone Mar 07 '12 at 00:39
  • Normally I'd agree, but it's a whole bunch of tiny parts that aren't really big enough to warrant an entire view. I'd end up with another 15+ views and a codebase about 20-30x larger. – Stephen Belanger Mar 07 '12 at 00:42

1 Answers1

1

you can do this in your initialize:

$(this.el).livequery(function() {
   // perform selector on $(this) to apply box
});

Shamelessly stolen and slightly modified from jquery live event for added dom elements

Community
  • 1
  • 1
timDunham
  • 3,318
  • 23
  • 24
  • Eww...I don't want to put in an entire third-party library for this. For now I'm just manually triggering a change event on the container everywhere that a content change is made from. Kind of hacky, but at least it keeps the event definitions standard and doesn't require relying on some third-party library--especially considering it hasn't been updated in 2 years! – Stephen Belanger Mar 07 '12 at 00:45
  • Plus it's based on livequery, which is just like jQuery `live`, which will be deprecated because of its evilness. – Eli Mar 07 '12 at 01:01
  • OK fair enough. I would add that if your backbone view isn't based on a model (which i assumed due to the change event not working) then there isn't really much that you're getting out of backbone views. It's really only a convetion (see first paragraph of http://documentcloud.github.com/backbone/#View) – timDunham Mar 07 '12 at 03:00