0

I have a span in my view which is rendered using Backbone.js

I want to get its HTML as soon as its rendered

Something like : $(span).html()

How can I do this?

Nope
  • 22,147
  • 7
  • 47
  • 72
Anshul
  • 806
  • 1
  • 7
  • 9
  • 4
    Please see [Backbone.js event after view.render() is finished](http://stackoverflow.com/questions/9790361/backbone-js-event-after-view-render-is-finished). See if that adds some insight. Also, I'd recommend adding more details to your question if that doesn't help clarify some things. – bdrelling Jan 23 '13 at 22:16

2 Answers2

1

This question confused me a bit, because Backbone.View doesn't actually have a render method at all (well, technically it does but its a no-op); it's 100% user-defined. Given that fact, checking ... well, anything after you render is as simple as ... well, checking it after you render.

In other words, if your view's render method is:

var YourView= Backbone.View.extend({
    render: function() {
        this.$el.html(someHtml);
    }
});

Then all you need to do is:

var YourView= Backbone.View.extend({
    render: function() {
        this.$el.html(someHtml);
        console.log(this.$el.html()); // check rendered HTML
    }
});

If you want to intermediate that with events you could (as @aerodynamo suggested):

var YourView= Backbone.View.extend({
    events: {'customPostRender': 'postRender'},
    postRender: function() {
        console.log(this.$el.html()); // check rendered HTML
    },
    render: function() {
        this.$el.html(someHtml);
        this.trigger('customPostRender');
    }
});

but really that's not even necessary.

machineghost
  • 33,529
  • 30
  • 159
  • 234
0

Not sure if MutationObserver helps, but definitely worth to check out:-)

geniuscarrier
  • 4,199
  • 2
  • 21
  • 15