{"id":227,"date":"2015-04-17T18:18:37","date_gmt":"2015-04-17T18:18:37","guid":{"rendered":"https:\/\/www.tech.shinynewthings.com\/?p=227"},"modified":"2015-04-17T18:59:57","modified_gmt":"2015-04-17T18:59:57","slug":"ember-js-image-component-with-backup-default-image","status":"publish","type":"post","link":"https:\/\/www.tech.shinynewthings.com\/?p=227","title":{"rendered":"Ember.js &#8211; Image Component with backup Default Image"},"content":{"rendered":"<p>Because of things outside of our control as developers, images don&#8217;t always load and the design suffers. This occurs because of a connectivity issues, or when an image path is incorrect or points to a resource that is no longer available.<\/p>\n<p>In the interest of graceful failure, I include a default image as a backup for images that don&#8217;t load.<\/p>\n<p>components\/default-img.js<\/p>\n<pre>import Ember from 'ember';\n\nexport default Ember.Component.extend({\n    tagName: 'img',\n    attributeBindings: ['src'],\n\n    didInsertElement: function(){\n        var _this = this;\n\n        \/\/ With jQuery\n        this.$().on('load', function(evt){\n            return _this.imageLoaded(evt);\n        }).on('error', function(evt){\n            return _this.errorLoading(evt);\n        });\n\n        \/\/ Without jQuery \/ for Mobile\n        \/*\n        var domRef = document.getElementById(this.get('elementId'));\n        domRef.addEventListener('load', function (evt) {\n            return _this.imageLoaded(evt);\n        }, false);\n        domRef.addEventListener(\"error\", function (evt) {\n            return _this.errorLoading(evt);\n        }, false);\n        *\/\n    },\n\n    willDestroyElement: function(){\n        this.$().off('load', 'error');\n    },\n\n    imageLoaded: function(event){\n        console.log(\"Image Loaded\");\n    },\n\n    errorLoading: function(event){\n        this.set('src', this.get('defaultImg'));\n    }\n\n});\n<\/pre>\n<p><strong>Use:<\/strong><\/p>\n<p>Controller:<\/p>\n<pre>\/\/ path to the Expected image\nvar img = data.imgPath; \n\n\/\/ path to Default \/ backup image\n\/\/ change this to match design requirements or default image path.\nvar defaultImg = 'http:\/\/placehold.it\/190x200'; \n\n<\/pre>\n<p>View:<\/p>\n<pre>{{default-img src=img defaultImg=defaultImg}}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Because of things outside of our control as developers, images don&#8217;t always load and the design suffers. This occurs because of a connectivity issues, or when an image path is incorrect or points to a resource that is no longer available. In the interest of graceful failure, I include a default image as a backup [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=227"}],"version-history":[{"count":7,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":234,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=\/wp\/v2\/posts\/227\/revisions\/234"}],"wp:attachment":[{"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech.shinynewthings.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}