Simon
Simon

Reputation: 592

Reset a route's model

I have a route that is responsible for creating transactions in a finance app. When the user has added a transaction they may well wish to enter some more so I don't transition away. My question is, how do I reset the model attached to the route so it's a brand new record (I'm using ember data)?

Upvotes: 3

Views: 2476

Answers (2)

Jose Rojas
Jose Rojas

Reputation: 3520

You can execute the model hook calling the refresh method in your route, this way:

App.TransactionRoute = Ember.Route.extend({
    model: function() {
    return this.store.createRecord('transaction');
    },
    actions: {
           newTransaction: function() {
                           this.refresh();
           }
    }
});

App.TransactionController = Ember.ObjectController.extend({
    actions: {
        save: function() {
              self = this;
              this.get('model').save().then(function(){
                     self.send('newTransaction');
              });
        }
    }
});

Upvotes: 0

Panagiotis Panagi
Panagiotis Panagi

Reputation: 10077

After saving the record, get a new record by calling the route's model() and set it to the model property of the corresponding controller:

App.TransactionRoute = Ember.Route.extend({
  model: function() {
    return this.store.createRecord('transaction');
  }

  actions: {
    save: function() {
      var model = this.modelFor('transaction');
      var controller = this.controllerFor('transaction');
      var route = this;

      model.save().then(function(){        
        var newModel = route.model();
        controller.set('model', newModel);
      });
    }
  }
});

Upvotes: 4

Related Questions