GithubHelp home page GithubHelp logo

Comments (6)

cmather avatar cmather commented on May 30, 2024

Yeah for the first case you can use the onRendered hook. For the second, I'm not sure what HCR means :-).

from iron-router.

tmeasday avatar tmeasday commented on May 30, 2024

Hot Code Reload.

Hopefully there'll be a way to do this without having to define a controller (I haven't had to explicitly do so so far!).

from iron-router.

tmeasday avatar tmeasday commented on May 30, 2024

To be clearer: it's a common use case to want to set "view" state for a page in the session.

For example, you might want to Session.set('sidebarOpen', false) every time you browse to a page with a sidebar in it.

If you browse away and then come back, you want the "view" to be reset, so the sidebar to be closed again. However, if there's a HCR, you do not want the view to be reset.

From the router's perspective, it's difficult to tell the difference. I don't think it's impossible though.

from iron-router.

cmather avatar cmather commented on May 30, 2024

Ah yes, Hot Code Reload. I thought about that issue a few times. My inclination is to allow components to handle this as each component will have scoped reactive state. I'm assuming David's implementation allows for HCR, but I haven't confirmed this.

Putting the per page state in the Router would require creating a global reactive dictionary that the router uses. When the page reloads, the route necessarily runs again, creating a new instance of the controller. So the controller instance reactive dictionary would not survive the HCR.

Maybe we should take a closer look at the Component implementation. So far I've only studied the base.js code from a week or so ago. I didn't see a scoped reactive dictionary, but David mentioned it in his talk.

from iron-router.

tmeasday avatar tmeasday commented on May 30, 2024

Ahh, yes. Well, without knowing details of how UI/Components might work I would hazard a guess that the right thing for the router to do would be register itself for migration and defer to the currently rendered controller to save it's state. Then when a "migrated" startup happens (i.e. after HCR), the first thing the router could do is read that data back out and use it to establish both it's own internal state and that of the rendered controller. After which it would check the URL and make sure everything is kosher with what it expects.

But that's pretty complex and out of scope, I am not suggesting that we do so now!

In the meantime whilst we are stuck using the Session for component state, I'm wondering if the following solution isn't to janky:

// just a little note so we know when we are starting up based on migration
Meteor._reload.onMigrate('iron-router', function() { return [true, Location.path()]; });

// When routing to a controller:

// we don't want to do this when it's the first route we are rendering and we are coming out of HCR
if (! (firstRoute && Meteor._reload.migrationData('iron-router') === Location.path())) {
  controller.beforeRoutingTo();
}
firstRoute = false;

Then I can write something like:

Router.map(function() {
  this.route('foo', {
    beforeRoutingTo: function() {
      Session.set('menuState', 'open');
    }
  });
});

And know for sure that menuState will always be set, but will not be reset when HCR happens.

from iron-router.

cmather avatar cmather commented on May 30, 2024

Assuming this is covered in your PR. Will look today.

from iron-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.