resthub / resthub-backbone-stack

RESThub Backbone.js stack
http://resthub.org/docs/backbone/
Other
56 stars 26 forks source link

When a 'root' is specified while instancing a new Resthub.View, the view's el is inserted in the DOM during the instanciation #145

Open notslackingatwork opened 10 years ago

notslackingatwork commented 10 years ago

Hi,

The title says it all. if you instanciate a view extending Resthub.View that has a "root" value defined in it, or if root is specified in the options object, this view's el will be attached to the root element during the instanciation, even if "render" is never called. I don't think this should be the case.

example : function (Resthub) { var Popin = Resthub.View.extend({ className: 'test', }); return Popin; });

new Popin(); --> $el is attached new Popin({root:"body"}); --> $el is atttached to the body during this call.

apparently, the el gets attached to the root element in the Resthub method "_insertRoot". The call stack is as follows :

(backbone.js) View ( http://backbonejs.org/docs/backbone.html#section-120 ) (backbone.js) _ensureElement ( http://backbonejs.org/docs/backbone.html#section-133) (resthub.js) setElement (resthub.js) _insertRoot

Maybe this behavior should be moved to the render ?

Thanks, Léo.

sdeleuze commented 10 years ago

Hi,

Thanks for reporting this issue. It is about extending Backbone.View or Resthub.View ?

Regards, Seb

bclozel commented 10 years ago

Resthub.View. Behavior is inconsistent depending on the arguments provided in the view constructor: root provided => $el attached in DOM; not provided => $el not attached.

notslackingatwork commented 10 years ago

yeah sorry I messed up. I'll edit my post

sdeleuze commented 10 years ago

I put a fix in my fork : https://github.com/sdeleuze/resthub-backbone-stack, but I am not sure we should merge it.

I am not sure it is inconsistent : if no root is provided, $el should be attached manually by the user (like in raw Backbone). If it is provided, it need to be attached to the DOM just 1 time. Nevertheless, this fix could perhaps make things better in cases where $el is removed from DOM between 2 render() calls.

Anyway, on non trivial apps this change has currently some side effects (need more tests to figure out exactly what occur).

Could you explain what issues current implementation produce, and test the fix I provided in my fork ?

notslackingatwork commented 10 years ago

The issue seems to be fixed with the fix.

The problem I was encountering was : I wanted to create a view which should be displayed or not depending on some conditions internal to the view itself.

So I created the view and called one of that view's method that would call render() or not depending on some conditions. What I expected was : if render() is never called, the view shouldn't be at all in the DOM. Instead, the $el was always in the DOM (due this particular $el's CSS, the whole page was covered by a transparent layer preventing any click, making it kind of hard to miss).

Of course there are a lot of ways to bypass my issue (the most obvious one being testing the conditions before making the new instance), but It was surprising to have the $el in the DOM when I though I didn't do any "rendering", and I thought that behaviour was not natural, so I let you know.