Skip to content
This repository has been archived by the owner on Apr 2, 2019. It is now read-only.

Can't re-render without breaking events #71

Closed
gregberge opened this issue Mar 19, 2013 · 10 comments
Closed

Can't re-render without breaking events #71

gregberge opened this issue Mar 19, 2013 · 10 comments
Labels
Milestone

Comments

@gregberge
Copy link

I can't re-render the grid without breaking events like click on th to sort columns.

The philosophy of Backbone is such that i can re-render several times without breaking UI.

Is there a solution ? Can you fix this ?

@wyuenho
Copy link
Contributor

wyuenho commented Mar 19, 2013

How do you trigger a re-render and what events on which part stop working?

@gregberge
Copy link
Author

For example, i do :

var MyView = new Backbone.View.extend({
    initialize: function () {
      View.prototype.initialize.apply(this, arguments);

      this.gridView = new Backgrid.Grid({
        collection: this.collection,
        el: '.backgrid'
      });
    },

    render: function () {
      View.prototype.render.apply(this, arguments);
      this.gridView.render();
      return this;
    }
});

var view = new MyView();
view.render(); // events OK
view.render(); // events breaked

@wyuenho
Copy link
Contributor

wyuenho commented Mar 19, 2013

What is in that ListGridView?

@gregberge
Copy link
Author

ListGridView is Backgrid.Grid, sorry i made a mistake.

@wyuenho
Copy link
Contributor

wyuenho commented Mar 19, 2013

I can't reproduce this from the demos. Either you show me all the relevant code or you'll have to write a failing test case to point out the error.

@gregberge
Copy link
Author

I find the origin of the problem, in each render you do a this.$el.empty(), but when you do that, jQuery unbinds all events. So there is two solutions :

  • rebind events in each render using this.delegateEvents
  • don't use empty

I make a fiddle to show you the problem.

@wyuenho
Copy link
Contributor

wyuenho commented Mar 19, 2013

Ah thanks for catching this, I just reproduced it. No need for a fiddle anymore.

@gregberge
Copy link
Author

Ok, but i have already made it http://jsfiddle.net/neoziro/WaBJF/

@wyuenho
Copy link
Contributor

wyuenho commented Mar 19, 2013

Thx. Will fix that tomorrow.

@gregberge
Copy link
Author

If you need help, don't hesitate to tell me.

An other solution is to always use setElement, in my projects i always include my subviews using this functions :

Backbone.View.extend({

  append: function (selector, view) {
    this.$(selector).append(view.render().el);
    view.delegateEvents();
    return this;
  },

  assign: function (selector, view) {
    view.setElement(this.$(selector)).render();
    return this;
  }
});

It can give you some ideas :).

Thanks for the fix and for this great job !

wyuenho added a commit that referenced this issue Mar 24, 2013
@wyuenho wyuenho closed this as completed Mar 24, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants