From 48d61af3a8d39c4bb3d02a5fb25a2cbf1320f76a Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Tue, 29 Sep 2015 15:53:38 +0200 Subject: [PATCH] Fix empty image when navigating to new editor from an editor with image closes #5840 - calls `reset()` on the uploader when re-rendered with a blank image - calls `reset()` and `initWithImage()` on the uploader when re-rendered with an image and the uploader is still in the "blank" state --- ghost/admin/app/components/gh-uploader.js | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ghost/admin/app/components/gh-uploader.js b/ghost/admin/app/components/gh-uploader.js index 96ef21e3db..3cc6d3ed29 100644 --- a/ghost/admin/app/components/gh-uploader.js +++ b/ghost/admin/app/components/gh-uploader.js @@ -52,6 +52,31 @@ export default Ember.Component.extend({ this.removeListeners(); }, + // NOTE: because the uploader is sometimes in the same place in the DOM + // between transitions Glimmer will re-use the existing elements including + // those that arealready decorated by jQuery. The following works around + // situations where the image is changed without a full teardown/rebuild + didReceiveAttrs: function (attrs) { + var oldValue = attrs.oldAttrs && Ember.get(attrs.oldAttrs, 'image.value'), + newValue = attrs.newAttrs && Ember.get(attrs.newAttrs, 'image.value'), + self = this; + + // always reset when we receive a blank image + // - handles navigating to populated image from blank image + if (Ember.isEmpty(newValue) && !Ember.isEmpty(oldValue)) { + self.$()[0].uploaderUi.reset(); + } + + // re-init if we receive a new image but the uploader is blank + // - handles back button navigating from blank image to populated image + if (!Ember.isEmpty(newValue) && this.$()) { + if (this.$('.js-upload-target').attr('src') === '') { + this.$()[0].uploaderUi.reset(); + this.$()[0].uploaderUi.initWithImage(); + } + } + }, + actions: { initUploader: function () { var ref,