greenymaster69
greenymaster69

Reputation: 1516

Ember makes unwanted call to backend in model hook

I want to be able to retrieve a certain conversation when its id is entered in the URL. If the conversation does not exist, I want to display an alert message with a record not found.

here is my model hook :

model: function(params){
    return this.store.filter('conversation', { status : params.status}, function(rec){
        if(params.status == 'all'){
            return ((rec.get('status') === 'opened' || rec.get('status') === 'closed'));
        }
        else{
            return (rec.get('status') === params.status); <--- Problem is here
        }
    });
}

For example, if I want to access a certain conversation directly, I could do :

dev.rails.local:3000/conversations/[email protected]#/convid

The problem is when I enter a conversation id which doesn't exist (like asdfasdf), ember makes call to an inexisting backend route.

It makes a call to GET conversation/asdfasdf. I'm about sure that it is only due to the record not existing. I have nested resources in my router so I'm also about sure that it tries to retrieve the conversation with a non existing id.

Basically, I want to verify the existence of the conversation before returning something from my hook. Keep in mind that my model hook is pretty much set and won't change, except for adding a validation on the existence of the conversation with the id in the url. The reason behind this is that the project is almost complete and everything is based on this hook.

Here is my router (some people are going to tell me you can't use nested resources, but I'm doing it and it is gonna stay like that so I have to work with it because I'm working on a project and I have to integrate ember in this section only and I have to use this setup) :

App.Router.map(function(){
    // Routing list to raw namespace path
    this.resource('conversations', { path : '/' }, function() {
        this.resource('conversation', { path : '/:conversation_id'});
    });
});

This also happens when I dont specify any id and I use the hashtag in my url like this :

dev.rails.local:3000/conversations/[email protected]#/ would make a call to conversation/

I know it is because of my nested resource. How can I do it?

Upvotes: 1

Views: 58

Answers (2)

greenymaster69
greenymaster69

Reputation: 1516

Ok so here is what I did. I removed my nested resource because I realised I wasn't using it for any good reason other than redirecting my url. I decided to manually redirect my url using javascript window.location.

This removed the unwanted call (which was caused by the nested resource).

Thanks to torazaburo, you opened my eyes on many things.

Upvotes: 0

user663031
user663031

Reputation:

By passing a query to filter (your { status : params.status}) you are asking Ember Data to do a server query. Try removing it.

From the docs at http://emberjs.com/api/data/classes/DS.Store.html#method_filter:

Optionally you can pass a query, which is the equivalent of calling find with that same query, to fetch additional records from the server. The results returned by the server could then appear in the filter if they match the filter function.

So, remove the query:

model: function(params){
    return this.store.filter('conversation', function(rec) {
        if (params.status == 'all') {
            return rec.get('status') === 'opened' || rec.get('status') === 'closed';
        } else {
            return rec.get('status') === params.status;
        }
    });
}

Upvotes: 3

Related Questions