lundi 15 juin 2015

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.l@email.com#/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.l@email.com#/ would make a call to conversation/

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

Aucun commentaire:

Enregistrer un commentaire