Dogstudio / highway

Highway - A Modern Javascript Transitions Manager
https://highway.js.org/
MIT License
1.43k stars 92 forks source link

Handling dynamic updates of view, such as ajax. #31

Closed zhw2590582 closed 6 years ago

zhw2590582 commented 6 years ago

In my project, the instance's cache can't handle ajax changes, I wonder if I should add a public method that lets the user manually update the view. Like:

  /**
   * Manually update the view.
   */
  updateCache() {
    const key = this.location.href;
    const cache = this.cache.get(key);
    if (cache) {
        cache.page = document.cloneNode(true);
        cache.view = document.querySelector('[data-router-view]').cloneNode(true);
        this.cache.set(key, cache);
    }
  }
Anthodpnt commented 6 years ago

Hi @zhw2590582,

I think this could be a good idea, can you just be more specific and describe a use case scenario ?

Thanks, Anthodpnt

zhw2590582 commented 6 years ago

Just sometimes, every time I enter the view, I don't want to re-ajax to modify the dom. I just want to ajax once and cache the result.

common.js

import Highway from "@dogstudio/highway/build/es5/highway";

export default new Highway.Core({
  renderers: {
    index: () => import(/* webpackChunkName: "index" */ "./index")
  }
});

index.js

import H from "./common";

export default class Renderer extends Highway.Renderer {
  onEnter() {
    console.log("onEnter");
  }
}

// Request data once
fetchData().then(data => {
  //  modify the dom something...
  //  then update the cache
  H.updateCache();
});
zhw2590582 commented 6 years ago

In my project, I don't think this is a good habit. Like:

export default class Renderer extends Highway.Renderer {
  // Request data every time
  onEnter() {
    fetchData().then(data => {
      //  modify the dom every time...
    });
  }
}
Anthodpnt commented 6 years ago

@zhw2590582 I'm not sure to understand why you need to fetch data when entering a page.

zhw2590582 commented 6 years ago

Going to the page and then fetch data just a very common requirement. It's like getting the remote list data and rendering the table dom or something. But the remote list data doesn't change often, I don't want to request data every time I enter the page. This is just my personal advice...😄

Anthodpnt commented 6 years ago

Ok so for example you come on a page, fetch datas from an API for example to display a list or something and you want to update the cache of Highway manually. But why don't you just check if the page is in cache and fetch the datas only if it's not instead of updating the cache ?

zhw2590582 commented 6 years ago

Because Highway is just caching the dom before ajax, not the dom after ajax, I think I can handle this problem., thank you for your reply, I can close this issue now. 😄