rksh
rksh

Reputation: 4050

Ember refresh route on form submit

I've a form in one of the ember routes when submitted i need to fresh only that,

App.PostController = Ember.ObjectController.extend({
    actions: {
      submit: function(e){
        var comment = this.get("ncomment");
        this.set("ncomment","");
        var self = this;
       $.post('/api/comment.php' , {comment : comment})
        .done(function(){
            self.refresh();
       });
      }
}
});

This code is giving me an error TypeError: self.refresh is not a function

How can I fix this?

Upvotes: 0

Views: 1218

Answers (2)

Microfed
Microfed

Reputation: 2890

To enter again on same route with same model you need to call Em.Route.refresh method on that route. To do it from Em.Controller you need to access to that route. Usually, you can do it by this.get('target') (inside controller). Then, controller must send action to route: this.get('target').send('actionName') (that action must be in actions hash of the route).

Complete example:

App.IndexRoute = Ember.Route.extend({
  actions: {
    refresh: function() {
      this.refresh();
    }
  }
});

App.IndexController = Ember.ObjectController.extend({
  actions: {
    ref: function(){
      this.get('target').send('refresh');
    }
  }
});

jsbin.

Upvotes: 1

prabhash
prabhash

Reputation: 56

Have a action on PostRoute that calls route's refresh() method and call that action from the controller.

Something like this.

App.PostController = Ember.ObjectController.extend({
    actions: {
      submit: function(e){
        var comment = this.get("ncomment");
        this.set("ncomment","");
        var self = this;
       $.post('/api/comment.php' , {comment : comment})
        .done(function(){
            self.send('refreshRoute');
       });
      }
 }
});

App.PostRoute = Ember.Route.extend({
  actions: {
   refreshRoute:function(){
    this.refresh()
   }
  }
});

Upvotes: 1

Related Questions