Search code examples
javascriptdata-bindingbackbone.jsrivets.js

Binding Rivets.js before element in the DOM


I have a backbone view that has render() called from its parent view returning the sub-view which is then appended to the DOM. The problem is that I can't run

rivets.bind(this.$el, {user: this.user});

within the initialize method of the subview because the element is not in the DOM yet. I would rather not have to run another method after render to bind with rivets.

Probably a simple problem but any ideas?


Solution

  • I'm using a very similar pattern of views with subviews, and Rivets 0.3.8 has had no problem binding to jQuery objects that are only inserted into the DOM some time after being bound.

    My render methods look something like this:

    function render() {
        this.setElement(_.template('<html>...</html>', {tem: plate}));
        rivets.bind(this.$el, {riv: ets});
        return this;
    }
    

    Then later:

    myView.render();
    $('#myTarget').empty();
    $('#myTarget').append(myView.el);