0

I wanna know if there is a way to call a function after rendering the view by backbone. The problem is that, the function I want to call need an html element rendered by backbone. indeed, I'm using 'ipywidgets' python module, which allows the creation of custom widgets which are exactly backbone views.

Here is the view code:

var TestView = widget.DOMWidgetView.extend({


    render: function() {
        var template = _.template( $("#editor_template").html(), {} );
        this.$el.html(template);
        this.model.on('change:value', this.backToFront, this);
        //return this;
    },


    backToFront: function() {
        //$("#editor").val(this.model.get('script_ldp'))
        editor.getSession().setValue(this.model.get('value'));
        console.log("python change value " + this.model.get('value'));
    },

    events: {'click':'frontToBack'},

    frontToBack: function(event){
        this.model.set('value', editor.getSession().getValue()); 
        this.touch();
        console.log("js change value " + this.model.get('value'));
    }});

this piece of code will display an html textarea on the page, and after-done, I should transform this textarea to something else(ace editor) by getting it from the DOM by it's 'id'.

If I do that: document.getElementById('textarea_id_returned_by_render') it returns null. I also tried that by it didn't work for me, it can be because of the python module I'm using, I mean that I'm not using pure backbone.js.

Cœur
  • 37,241
  • 25
  • 195
  • 267
DevDev
  • 313
  • 2
  • 12
  • 4
    Can you provide more details? Usually you'd put that code inside the view's `render` method. – mu is too short Mar 18 '16 at 18:07
  • 1
    It's unclear to what you're asking for. The implementation of `render()` is up to you by design. So, if you implement it, you may do any other tasks too. Do you need to call a function after rendering any of multiple views? If so is it always the same function or should it be configurable? Some (pseudo) code may help. – try-catch-finally Mar 18 '16 at 23:35
  • I edited the question. @mu is too short try-catch-finally thank u for help – DevDev Mar 21 '16 at 10:16
  • 1
    Normally you'd do whatever needs to be done inside `render` with something like `this.$('#textarea_id')` to grab the element you need. If you need to wait until the element is on the page (such as if you need to know how big it is) then you have to do it yourself after you call `render` or use a `setTimeout(callback, 0)` trick. – mu is too short Mar 21 '16 at 21:22
  • yep, setTimeout, that's exactly what I did and it worked fine. thank u – DevDev Mar 22 '16 at 09:25

0 Answers0