vendredi 24 mars 2017

Reading error status code in response header in ember

In my app I want to render some images.for that I am setting the path in my img src attribute.This will make a call to the server and on success the response is set to src attribute and the image is rendered.In some cases the server may respond with errors in response headers with status codes like 400,401,404,500.on error i will display a default image.I have handled this with onerror, onload methods.It is working perfectly in displaying default images onerror.But I want to read the status code on response headers in onerror method to differentiate between the errors.May I know how can i read the status code in response header?

image model file:

    import Ember from 'ember';  
    import DS from 'ember-data'; 
    export default DS.Model.extend({
    image_url:DS.attr(),
    is_image_created: computed('image_url', function(){ 
            let img = new Image(),
                self = this,
                url = "/api/image/"+self.get("id"); 
            img.onerror = img.onabort = function() {
                self.set("is_image_created", false); 
            };
            img.onload = function() {
                self.set("is_image_created", true); 
            };
            run.later(function(){
                img.src = url;
            }, 3);
        }),
    });

displayimage.js---route file

import Ember from 'ember'; 

const { Route} = Ember;

export default Route.extend({
model(params){
return this.store.findRecord('image',params.id);
},
});

displayimage.hbs


                <img src="/api/image/"+model.id>
            
                <svg class=" inline-block zdicon-26">
                     <use xlink:href="#icon-"> </use>
                </svg>
            

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire