From c6363b11c78157d7d698147da1f5c2275556851c Mon Sep 17 00:00:00 2001 From: Jared Cobb Date: Thu, 24 Aug 2017 16:04:43 -0600 Subject: [PATCH 1/2] Submits all shortcode fields for AJAX searches. --- js/build/shortcode-ui.js | 3 ++- js/src/views/select2-field.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 893fb75d..8412200a 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -1851,7 +1851,8 @@ sui.views.editAttributeSelect2Field = sui.views.editAttributeField.extend( { s : params.term, // search term page : params.page, shortcode : self.shortcode.get( 'shortcode_tag'), - attr : self.model.get( 'attr' ) + attr : self.model.get( 'attr' ), + fields : self.$el.parents('form').serialize() }, self.ajaxData ); }, processResults: function (response, params) { diff --git a/js/src/views/select2-field.js b/js/src/views/select2-field.js index ece037d6..85497d59 100644 --- a/js/src/views/select2-field.js +++ b/js/src/views/select2-field.js @@ -117,7 +117,8 @@ sui.views.editAttributeSelect2Field = sui.views.editAttributeField.extend( { s : params.term, // search term page : params.page, shortcode : self.shortcode.get( 'shortcode_tag'), - attr : self.model.get( 'attr' ) + attr : self.model.get( 'attr' ), + fields : self.$el.parents('form').serialize() }, self.ajaxData ); }, processResults: function (response, params) { From 25f8f11621686511f235b46c24b9dc647f53b1ff Mon Sep 17 00:00:00 2001 From: Nathaniel Taintor Date: Fri, 25 Aug 2017 13:11:14 -0700 Subject: [PATCH 2/2] Wrap ajax data for select2 search requests in a filter Adds a hook where the data sent along with the search request for a select2 field can be filtered, for cases where the user wants to use the value of one field to affect the search results returned for another. --- js/build/shortcode-ui.js | 24 ++++++++++++++++++++++-- js/src/views/select2-field.js | 24 ++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/js/build/shortcode-ui.js b/js/build/shortcode-ui.js index 8412200a..b6d1cdac 100644 --- a/js/build/shortcode-ui.js +++ b/js/build/shortcode-ui.js @@ -1847,13 +1847,33 @@ sui.views.editAttributeSelect2Field = sui.views.editAttributeField.extend( { dataType: 'json', delay: 250, data: function (params) { + + var shortcode = self.shortcode.get( 'shortcode_tag' ), + attr = self.model.get( 'attr' ), + filterName = [ shortcode, attr, 'ajax-data' ].join('.'), + collection = _.flatten( _.values( self.views.parent.views._views ) ); + + /** + * Filter any additional parameters sent along with search request. + * + * Called as `{shortcodeName}.{attributeName}.ajax-data`. + * + * @param ajaxData (object) + * Default fields sent with request + * @param viewModels (array) + * The collections of views (editAttributeFields) + * which make up this shortcode UI form + * @param shortcode (object) + * Reference to the shortcode model which this attribute belongs to. + */ + var ajaxData = wp.shortcake.hooks.applyFilters( filterName, self.ajaxData, collection, self.shortcode ); + return $.extend( { s : params.term, // search term page : params.page, shortcode : self.shortcode.get( 'shortcode_tag'), attr : self.model.get( 'attr' ), - fields : self.$el.parents('form').serialize() - }, self.ajaxData ); + }, ajaxData ); }, processResults: function (response, params) { if ( ! response.success || 'undefined' === typeof response.data ) { diff --git a/js/src/views/select2-field.js b/js/src/views/select2-field.js index 85497d59..bfea2220 100644 --- a/js/src/views/select2-field.js +++ b/js/src/views/select2-field.js @@ -113,13 +113,33 @@ sui.views.editAttributeSelect2Field = sui.views.editAttributeField.extend( { dataType: 'json', delay: 250, data: function (params) { + + var shortcode = self.shortcode.get( 'shortcode_tag' ), + attr = self.model.get( 'attr' ), + filterName = [ shortcode, attr, 'ajax-data' ].join('.'), + collection = _.flatten( _.values( self.views.parent.views._views ) ); + + /** + * Filter any additional parameters sent along with search request. + * + * Called as `{shortcodeName}.{attributeName}.ajax-data`. + * + * @param ajaxData (object) + * Default fields sent with request + * @param viewModels (array) + * The collections of views (editAttributeFields) + * which make up this shortcode UI form + * @param shortcode (object) + * Reference to the shortcode model which this attribute belongs to. + */ + var ajaxData = wp.shortcake.hooks.applyFilters( filterName, self.ajaxData, collection, self.shortcode ); + return $.extend( { s : params.term, // search term page : params.page, shortcode : self.shortcode.get( 'shortcode_tag'), attr : self.model.get( 'attr' ), - fields : self.$el.parents('form').serialize() - }, self.ajaxData ); + }, ajaxData ); }, processResults: function (response, params) { if ( ! response.success || 'undefined' === typeof response.data ) {