spine / spine

Lightweight MVC library for building JavaScript applications

Home Page:http://spine.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

jQuery Object Lost when clear the global dom scope

lujiajing1126 opened this issue · comments

When you use

 $('body').html require('views/some-template')

then you change to the controller by using event-trigger instead of re-instantiating the controller(usually recall constructor function)

// projectsController.coffee
@navigate '/projects'
// index.coffee
@routes
    "": ->
    @html require('views/index')
    "/login": ->
    @loginController = new Login(el: $('body'))
    "/projects": ->
    @projectController.active()

the @el will not pointer to the dom even if the dom existed

I will fix it later in the refreshElements of Spine.coffee
by re-finding the DOM using the old id or classes

so, this only is a problem if replacing the element that the spine app is initially set up to work from or one higher in the DOM tree?

I don't think this is a problem that should be solved in Controller::refreshElements().

If controller A has a child controller B, controller A should be responsible for either releasing & re-instantiating controller B, or detaching & re-appending controller B's view when replacing it's own view. Controller B should do the same so that child views are kept in sync recursively.

If you're replacing the entire document body without letting any controllers know... don't do that :)

The solution proposed in #567 adds a new requirement that all views have a unique ID or class, which isn't practical and will likely break a lot of people's code. It also searches the entire DOM each time it's called which is inefficient.

I've been working on a module that helps manage child controllers which I hope to submit a PR for soon. Something like that might help with @lujiajing1126's problem. But for now I'd suggest he create a controller to manage the <body> element, and manage child controllers from there.

I was thinking a similar thing. "don't do that"

Think I will close this. As @adambiggs mentioned if you want to replace controllers nested inside of other controllers you should manage that explicitly. There is just to much to go wrong for Spine to try to handle that for you

@adambiggs Thx U for the help.
I may not understand the philosophy of the Spine architecture very well. And I admit my solution is less efficient but I still feel uncomfortable while handling such problem so that i couldn't substitute the whole body dom free.
I haven't encountered such problem in EmberJS because of another fully different view handle solution. So I wonder if the design of the Spine View Module is not so reasonable.