Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous data hook #7

Closed
holic opened this issue May 23, 2015 · 17 comments
Closed

Asynchronous data hook #7

holic opened this issue May 23, 2015 · 17 comments

Comments

@holic
Copy link

holic commented May 23, 2015

I'm finding myself doing a deep watch of router.params in all of my components used by <router-view> in order to reload the data loaded/initialized in my components' created handler. Is there a better way about this? Should this automatically happen for navigation changes in the same router component?

@holic holic changed the title Recreate components on navigation with same router-view Recreate components on URL change May 23, 2015
@yyx990803
Copy link
Member

There is going to be a data option to route definitions, which allows you to load some data based the the route before actually initializing/updating your components.

@yyx990803
Copy link
Member

Btw, thanks for these issues! It's really helpful to have feedback based on actual usage even before it's released :)

@holic
Copy link
Author

holic commented May 23, 2015

👍

How do you plan to handle async data? In my case, I'm loading from an external data source.

If I navigate to a page within the same component, causing a new set of data to be loaded, what happens between the navigation and data loading? Does the page remain unchanged until the data is loaded? Will there be an option to clear the data? I'd like to keep the UI feeling snappy, and if there's a delay between page updates after navigating without a way to clear the data, it'll feel really sluggish.

@yyx990803
Copy link
Member

What do you mean by "clear the data"?

@holic
Copy link
Author

holic commented May 23, 2015

Currently a VM's data is not cleared or reloaded on navigation changes within the same component (e.g. navigating from /entries/1 to /entries/2 where entries is the shared component). In order to load new data, I'm watching for changes in the route params, then fetching new data based on the new route params. Before that data is fetched, I'm clearing the data set on the last fetch so I can conditionally display a "loading" indicator (so that navigation has a clear effect on the page).

@yyx990803
Copy link
Member

There's an option called alwaysRefresh (maybe could use a better name) which basically means if the params change this component should just be reloaded.

@yyx990803
Copy link
Member

Although I haven't really put enough thought to the async data handling part yet - maybe there could be a more elegant solution.

@yyx990803
Copy link
Member

Alternatively - we can expose a loading flag on the route object to indicate that the async data function is still awaiting its result.

@yyx990803 yyx990803 changed the title Recreate components on URL change data hook Jun 7, 2015
@yyx990803 yyx990803 changed the title data hook Asynchronous data hook Jun 7, 2015
@fnlctrl
Copy link
Member

fnlctrl commented Oct 1, 2015

alwaysRefresh and loading are not implemented, so should I just watch the $route.params for now?

@fnlctrl
Copy link
Member

fnlctrl commented Oct 1, 2015

By the way how exactly should I watch vm.$route.params ?
I tried passing it to vm.$data.params

data: {
    params: vm.$route.params
}

and

vm.$watch('params', callback, {deep: true})

does not work.

@fnlctrl
Copy link
Member

fnlctrl commented Oct 1, 2015

The problem is that vm.$route.params was never updated when url changes from /entries/1/ to /entries/2/...

@yyx990803

@rleger
Copy link

rleger commented Dec 21, 2015

@yyx990803
Same here. I'm trying to 'force' reload my page components when switching from route
whatever/1 to whatever/2.

How would you go about to watch the $route.params anyway ?

Thanks

@benwalks
Copy link

+1 Also interested in refreshing component data based on a change in param.

@benwalks
Copy link

benwalks commented Jan 1, 2016

Did you find a solution to this @rleger? I'm still trying to reload a component's data upon a param change.

@rleger
Copy link

rleger commented Jan 2, 2016

@benwalks the best I could come up with is to watch the route params.. not elegant but for lack of a better solution it works.

ready() {
  this.$watch('$route.params', this.handleChange , { deep: true })
}

@fnlctrl
Copy link
Member

fnlctrl commented Jan 2, 2016

Well guys.. there has been a solution using route.data, read about it here, just as Evan wrote when he closed the issue on 11 Jun 2015. Maybe he was too busy to explain it here..

The reason the component isn't reloaded when route change from whatever/1 to whatever/2 is that their route.canReuse is set to true by default, so they are reused and the lifecycle hooks were never fired again. A straightforward way would be to set canReuse to false, but using route.data would be the best solution.

On 1 Oct 2015 I hadn't read though the docs carefully, so I posted the problems (which weren't) here. Later I read the docs and solved the problems, and totally forgot about this issue.... So I hope this helps @benwalks @rleger

@benwalks
Copy link

benwalks commented Jan 3, 2016

Yeah, thanks! I did actually figure that out in the end. Here's some sample code for future people.

export default {
    props: ['lessonNumber'],
    data() {
      return {
        lesson: {
          title: "",
          video: "",
          description: ""
        }
      }
    },
    ready() {
      this.fetchNewLesson()
    },
    methods: {
      fetchNewLesson() {
        this.$http.get('http://localhost:4000/api/v1/lessons/' + this.lessonNumber).then(function (response) {
          this.$set('lesson', response.data.data)
        })
      }
    },
    watch: {
      lessonNumber: {
        handler: function() { this.fetchNewLesson() },
        deep: true
      }
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants