Search code examples
ember.jsember-datarefreshember-cli

Ember: issue when refreshing the index page


When I refresh the index page of my ember app, it appears that I can't call the functions inside my index.js file located in app/routes.

I don't really know how I can solve this issue.

The source code of index.js:

import Ember from 'ember';
import ProjectRoute from 'web/project-route';

export default ProjectRoute.extend({
    authSrv: Ember.inject.service('authentication'),
    _title: 'index.title',
    _requireAuth: true,
    beforeModel()
    {
        "use strict";
        this._super();
    },
    model()
    {
        "use strict";
        console.log(this.get('authSrv').username);
        return {user_id: this.get('authSrv').user_id,
            username: this.get('authSrv').username};
    }
});

In the code source above we can see that I try to display the username. When I first log onto this page, it display well, but when I refresh the page, it doesn't display anything.
Any thought about it is welcomed!


Solution

  • So I fixed it with the Ember module RSVP. Basically, the main problem was coming from a promise. I didn't waited to catch the promise. The index.js look like this know.

    import Ember from 'ember';
    import ProjectRoute from 'web/project-route';
    
    export default ProjectRoute.extend({
        authSrv: Ember.inject.service('authentication'),
        _title: 'index.title',
        _requireAuth: true,
        beforeModel()
        {
            "use strict";
            this._super();
        },
        model()
        {
            "use strict";
            let promise = new Ember.RSVP.Promise((resolve, reject) =>
            {
                this.get('authSrv').get('current_auth').promise.then((res) =>
                {
                    resolve({user_id: this.get('authSrv').user_id,
                        username: this.get('authSrv').username});
                });
            });
            return promise;
        }
    });