Rojke
Rojke

Reputation: 47

How to refresh a model after a request to API

//Route 
url:  "https://xxxxxx.com/api/entries",
users: "https://xxxxxx.com/api/users/",
model: function(){
    var localData = JSON.parse(localStorage.getItem("user"));
    var data = { auth_token: localData.user_token };
    return new Ember.RSVP.hash({
        logs: Ember.$.ajax({ url:  this.get('url'), headers: { "X-Api-Token": data.auth_token } }),
        team: Ember.$.ajax({ url: this.get('users'), headers: { "X-Api-Token": data.auth_token } })
    }); 
}


//controller
actions:{
    deleteWorklog: function( worklogId ){
          var model = this.get('model.logs');
          var data = { auth_token: this.get('local_data').user_token };
          Ember.$.ajax({
               method: "DELETE",
               url: this.get('url') + "/" + worklogId,
               headers: { 'X-Api-Token': data.auth_token }
          }).then(function(data){
             //how do i do it?
          })
    },
   loadMore: function(){
        var model = this.get('model.logs');
        var url = this.get('url');
        var today = new Date();
        today.setDate(today.getDate() - this.get('from'));
        console.log(today);

        url += "?from="+ today.toISOString();

        Ember.$.ajax({ 
            url: url,
            headers: { "X-Api-Token": data.auth_token } 
         }).then(function(data) { 
            model.replace(0, model.length, data); 
         });

        var initial = this.get('from') + 10;
        this.set('from', initial);
    }
}
}

I'm blocked after the request, i need to refresh my model but i've to do cmd + R to see the change, is there a method re-call the model or something like that?

I've added another things maybe help

Upvotes: 2

Views: 2792

Answers (2)

GJK
GJK

Reputation: 37369

There's two ways that I can think of. The first is just to call the refresh method on the route. This is by far the simplest. Just call it and Ember will re-call the model hook for that route and any child routes. In your case, I would send an action from your controller to your route, then have your route refresh itself in that action handler.

The second way would be to manually re-get your data and set it on the controller. Perhaps something like this:

// route.js
actions: {
    refreshModel: function() {
        var route = this;
        Ember.$.ajax({ ... }).then(function(data) {
            route.get('controller').set('model', data);
        });
    }
}

However, I wouldn't recommend this method over the first. There are too many variables when dealing with Ember routing and it's easier just to let Ember handle it all.

Upvotes: 1

enspandi
enspandi

Reputation: 663

You either want to use model.reload to refresh the record from the server: http://emberjs.com/api/data/classes/DS.Model.html#method_reload

Or you may want to delete the record local: http://emberjs.com/api/data/classes/DS.Model.html#method_deleteRecord

Or you may want to use model.destroyRecord to let Ember do the delete request (remote + local): http://emberjs.com/api/data/classes/DS.Model.html#method_destroyRecord

I think it would be the best to use model.destroyRecord, but I assume there's a reason you do this manually?

Upvotes: 0

Related Questions