0
votes

I have a simple route calling

model: function(params) {
  return this.store.find("booking", params.booking_id);
},

When the route is visited, changes can be made, and I want them to be reset the next time the page is visited during the same session, unless they have been saved. The simple operation of refreshing the data once obtained with { reload: true } now doesn't seem to have an easy, straightforward alternative.

How do I do this? Thank you.

2

2 Answers

1
votes

To refresh your ember model you can use this.refresh();.

To rollback all the attributes when you leave the route you can use this.controller.get('model').rollbackAttributes();

lets assume there is a route called post

In your routes/post.js

...
export default Route.extend({
  ...
  model: function() {
    ....
  },
  actions: {
    refreshModel: function() {
      this.refresh();
    },
    willTransition() {
      this.controller.get('model').rollbackAttributes();
    }
  }

call refreshModel action from your controller as this.sendAction('refreshModel')

1
votes

Your route has a dynamic segment. When you generate a link to that route you have the option to pass either a model context or id to the route.

If you pass a model context the model() hook will not fire and ember will use whatever value it currently holds in the store. If you pass model.id the model() hook will fire and fetch the latest data.

Check your link generation to this route and see if you are passing booking or booking.id to the route.

https://guides.emberjs.com/release/routing/specifying-a-routes-model/