From ce1f04bb48b900d7d61bcba5f98c9c1ad53e2081 Mon Sep 17 00:00:00 2001 From: Nathaniel Taintor Date: Thu, 26 Jan 2017 11:49:47 -0800 Subject: [PATCH 1/3] Re-add deprecated function Up until 0.7.0, we had a public class method exposed on editAttributeFieldAttachment called `getFromCache`, which was being used elsewhere to hook into attachment preview rendering and other useful attachment needs. (For example, the Image Shortcake plugin was using it to pull alt, caption, and other values from the attachment to prepopulate those fields.) I think it would still be helpful to expose this cache, even though it isn't needed anymore internally, just because it costs next to nothing to have in place, and is useful for integration with other fields. --- js/build/shortcode-ui.js | 49 ++++++++++++++++++- .../views/edit-attribute-field-attachment.js | 49 ++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index a3c466f0..8e80aedf 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -724,6 +724,8 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { self.initSelection(); + self.currentSelection.on( 'all', this.updateCache ); + self.currentSelection.on( 'all', this.updateValue ); self.currentSelection.on( 'add', this._renderPreview ); self.currentSelection.on( 'reset', this._renderAll ); @@ -754,7 +756,7 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { this.currentSelection.add( model ); - // Re-render after attachments have synced. + // Re-render after attachments have synced, and add to cache. model.fetch(); model.on( 'sync', this._renderAll ); @@ -762,6 +764,20 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { }, + /** + * Updates any fetched attachment models in the class attachment cache. + * + * This cache is exposed for convenience in listener functions that need to + * access fields from it. + * + * @return null + */ + updateCache: function() { + _.each( this.currentSelection.models, function( model ) { + editAttributeFieldAttachment.addToCache( model.attributes.id, model.attributes ); + } ); + }, + /** * Update the field attachment. * Re-renders UI. @@ -909,6 +925,37 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { }, +}, { + + _idCache: {}, + + /** + * Set a fetched attachment model in the _idCache lookup. + * + * @param int Attachment ID + * @param {Object} attachment model attributes + */ + addToCache: function( id, attachment ) { + this._idCache[ id ] = attachment; + }, + + /** + * Get an attachment model from the _idCache lookup. + * + * Prior to 0.7.0, this method was exposed as a public class + * method and used internally to get attachment details from the `_idCache` + * store. + * + * @deprecated Not used internally since 0.7.0 + */ + getFromCache: function( id ) { + if ( 'undefined' === typeof this._idCache[ id ] ) { + return false; + } + + return this._idCache[ id ]; + }, + }); module.exports = sui.views.editAttributeFieldAttachment = editAttributeFieldAttachment; diff --git a/js/src/views/edit-attribute-field-attachment.js b/js/src/views/edit-attribute-field-attachment.js index 17a770e8..dcbe2189 100644 --- a/js/src/views/edit-attribute-field-attachment.js +++ b/js/src/views/edit-attribute-field-attachment.js @@ -22,6 +22,8 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { self.initSelection(); + self.currentSelection.on( 'all', this.updateCache ); + self.currentSelection.on( 'all', this.updateValue ); self.currentSelection.on( 'add', this._renderPreview ); self.currentSelection.on( 'reset', this._renderAll ); @@ -52,7 +54,7 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { this.currentSelection.add( model ); - // Re-render after attachments have synced. + // Re-render after attachments have synced, and add to cache. model.fetch(); model.on( 'sync', this._renderAll ); @@ -60,6 +62,20 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { }, + /** + * Updates any fetched attachment models in the class attachment cache. + * + * This cache is exposed for convenience in listener functions that need to + * access fields from it. + * + * @return null + */ + updateCache: function() { + _.each( this.currentSelection.models, function( model ) { + editAttributeFieldAttachment.addToCache( model.attributes.id, model.attributes ); + } ); + }, + /** * Update the field attachment. * Re-renders UI. @@ -207,6 +223,37 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { }, +}, { + + _idCache: {}, + + /** + * Set a fetched attachment model in the _idCache lookup. + * + * @param int Attachment ID + * @param {Object} attachment model attributes + */ + addToCache: function( id, attachment ) { + this._idCache[ id ] = attachment; + }, + + /** + * Get an attachment model from the _idCache lookup. + * + * Prior to 0.7.0, this method was exposed as a public class + * method and used internally to get attachment details from the `_idCache` + * store. + * + * @deprecated Not used internally since 0.7.0 + */ + getFromCache: function( id ) { + if ( 'undefined' === typeof this._idCache[ id ] ) { + return false; + } + + return this._idCache[ id ]; + }, + }); module.exports = sui.views.editAttributeFieldAttachment = editAttributeFieldAttachment; From c05930c49a45ab2656d052bbd752fcb4bf90ed1a Mon Sep 17 00:00:00 2001 From: Nathaniel Taintor Date: Thu, 26 Jan 2017 12:49:59 -0800 Subject: [PATCH 2/3] Ensure that value is set before updating in cache Prevent errors where the updateCache callback is triggered before setValue is called. --- js/build/shortcode-ui.js | 3 +-- js/src/views/edit-attribute-field-attachment.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 8e80aedf..93bbcb8f 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -724,8 +724,6 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { self.initSelection(); - self.currentSelection.on( 'all', this.updateCache ); - self.currentSelection.on( 'all', this.updateValue ); self.currentSelection.on( 'add', this._renderPreview ); self.currentSelection.on( 'reset', this._renderAll ); @@ -788,6 +786,7 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { updateValue: function() { var value = this.currentSelection.pluck( 'id' ); this.setValue( value ); + this.updateCache(); this.triggerCallbacks(); }, diff --git a/js/src/views/edit-attribute-field-attachment.js b/js/src/views/edit-attribute-field-attachment.js index dcbe2189..56d8466f 100644 --- a/js/src/views/edit-attribute-field-attachment.js +++ b/js/src/views/edit-attribute-field-attachment.js @@ -22,8 +22,6 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { self.initSelection(); - self.currentSelection.on( 'all', this.updateCache ); - self.currentSelection.on( 'all', this.updateValue ); self.currentSelection.on( 'add', this._renderPreview ); self.currentSelection.on( 'reset', this._renderAll ); @@ -86,6 +84,7 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { updateValue: function() { var value = this.currentSelection.pluck( 'id' ); this.setValue( value ); + this.updateCache(); this.triggerCallbacks(); }, From 51a37d929cccf6377f445111c17af7e5022c715b Mon Sep 17 00:00:00 2001 From: Nathaniel Taintor Date: Thu, 26 Jan 2017 15:24:26 -0800 Subject: [PATCH 3/3] Simplify getFromCache to just look up using wp.media API We don't really have to start our own js object store to cache these attachments, because wp.media already stores it's own client-side cache. This removes the addToCache function, and just swaps out getFromCache to use wp.media.attachment under the hood. --- js/build/shortcode-ui.js | 40 +++---------------- .../views/edit-attribute-field-attachment.js | 40 +++---------------- 2 files changed, 12 insertions(+), 68 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 93bbcb8f..095f7ae1 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -762,20 +762,6 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { }, - /** - * Updates any fetched attachment models in the class attachment cache. - * - * This cache is exposed for convenience in listener functions that need to - * access fields from it. - * - * @return null - */ - updateCache: function() { - _.each( this.currentSelection.models, function( model ) { - editAttributeFieldAttachment.addToCache( model.attributes.id, model.attributes ); - } ); - }, - /** * Update the field attachment. * Re-renders UI. @@ -786,7 +772,6 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { updateValue: function() { var value = this.currentSelection.pluck( 'id' ); this.setValue( value ); - this.updateCache(); this.triggerCallbacks(); }, @@ -926,33 +911,20 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { }, { - _idCache: {}, - /** - * Set a fetched attachment model in the _idCache lookup. - * - * @param int Attachment ID - * @param {Object} attachment model attributes - */ - addToCache: function( id, attachment ) { - this._idCache[ id ] = attachment; - }, - - /** - * Get an attachment model from the _idCache lookup. + * Get the Backbone model attributes of an attachment. * * Prior to 0.7.0, this method was exposed as a public class - * method and used internally to get attachment details from the `_idCache` - * store. + * method and used internally to get attachment details from + * the `_idCache` store. * * @deprecated Not used internally since 0.7.0 */ getFromCache: function( id ) { - if ( 'undefined' === typeof this._idCache[ id ] ) { - return false; + if ( wp.media.attachment( id ) ) { + return wp.media.attachment( id ).attributes; } - - return this._idCache[ id ]; + return false; }, }); diff --git a/js/src/views/edit-attribute-field-attachment.js b/js/src/views/edit-attribute-field-attachment.js index 56d8466f..a66ebb0e 100644 --- a/js/src/views/edit-attribute-field-attachment.js +++ b/js/src/views/edit-attribute-field-attachment.js @@ -60,20 +60,6 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { }, - /** - * Updates any fetched attachment models in the class attachment cache. - * - * This cache is exposed for convenience in listener functions that need to - * access fields from it. - * - * @return null - */ - updateCache: function() { - _.each( this.currentSelection.models, function( model ) { - editAttributeFieldAttachment.addToCache( model.attributes.id, model.attributes ); - } ); - }, - /** * Update the field attachment. * Re-renders UI. @@ -84,7 +70,6 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { updateValue: function() { var value = this.currentSelection.pluck( 'id' ); this.setValue( value ); - this.updateCache(); this.triggerCallbacks(); }, @@ -224,33 +209,20 @@ var editAttributeFieldAttachment = sui.views.editAttributeField.extend( { }, { - _idCache: {}, - /** - * Set a fetched attachment model in the _idCache lookup. - * - * @param int Attachment ID - * @param {Object} attachment model attributes - */ - addToCache: function( id, attachment ) { - this._idCache[ id ] = attachment; - }, - - /** - * Get an attachment model from the _idCache lookup. + * Get the Backbone model attributes of an attachment. * * Prior to 0.7.0, this method was exposed as a public class - * method and used internally to get attachment details from the `_idCache` - * store. + * method and used internally to get attachment details from + * the `_idCache` store. * * @deprecated Not used internally since 0.7.0 */ getFromCache: function( id ) { - if ( 'undefined' === typeof this._idCache[ id ] ) { - return false; + if ( wp.media.attachment( id ) ) { + return wp.media.attachment( id ).attributes; } - - return this._idCache[ id ]; + return false; }, });