VelNaga VelNaga - 3 months ago 36
Javascript Question

Ember How to retain query parameters while manually refreshing the page?

I am using ember 2.7.0.while manually refreshing the page ember clears the ember-data as well us query parameters, so i am unable to load the page in setupController while refreshing. Is there any possible way to retain both model & query parameters, at least retaining query parameter would be fine to reload my page.

route.js

model(params) {
return this.store.peekRecord("book",params.book_id);
},

setupController(controller,model,params){
if(!model){
//fetch book record again if the model is null
}
controller.set('isdn',params.queryParams.isdn);
controller.set('book',model);
}


Any help should be appreciable.

Edited setupController as per Adam Cooper comment :

setupController(controller,model,params){
var isdn = params.queryParams.msisdn;
controller.set('isdn',isdn);
if(!model){
this.store.findRecord('book', isdn).then((customer) => {
this.set('book',customer);
},(resp,status) => {
this.set('errorMessage', `Book with this ${isdn} does not exist.`);
this.set('book', []);
});
}else{
controller.set('device',model);
}
}


Page gets rendered before "findRecord" returning promise.Is there any way to stop page rendering till find record resolves the promise?

Answer

You are setting in route properties instead of controller..

setupController(controller, model, params){
var isdn = params.queryParams.msisdn;
controller.set('isdn', isdn);
if(!model){
    this.store.findRecord('book', isdn).then((customer) => {
    controller.set('book', customer);
}, (resp, status) => {
    controller.set('errorMessage', `Book with this ${isdn} does not exist.`);
    controller.set('book', []);
});
}else{
    controller.set('device', model);
}

}

Only the controller properties will decorate template.

You can even try the below, why don't you give opportunity to model hook to resolve since that will wait for the Promises to resolve.

model(params) {   
    var result = this.store.peekRecord("book",params.book_id);
    if(result !== null){
       result= this.store.findRecord('book', params.book_id)
    }
  return result;
}
setupController(controller,model){    
    controller.set('book',model);
}