let SearchForm = Backbone.View.extend({ el: '#aff-amazon-import-search-form', events: { 'change select[name="type"]': 'change', 'change select[name="category"]': 'change', 'submit': 'submit', }, initialFocus: false, /** * Initialize the search form. * * @since 0.9 * @public */ initialize() { let templateHtml = jQuery('#aff-amazon-import-search-form-template').html(), providerConfigured = this.$el.data('provider-configured'); this.template = _.template(templateHtml); this.model.set('providerConfigured', providerConfigured === true || providerConfigured === 'true'); this.model.on('change', this.render, this); }, /** * Render the search form. * * @since 0.9 * @returns {SearchForm} * @public */ render() { this.$el.html(this.template(this.model.attributes)); let term = this.$el.find('input[name="term"]'), type = this.$el.find('select[name="type"]'), category = this.$el.find('select[name="category"]'), minPrice = this.$el.find('input[name="min-price"]'), maxPrice = this.$el.find('input[name="max-price"]'), condition = this.$el.find('select[name="condition"]'), sort = this.$el.find('select[name="sort"]'), withVariants = this.$el.find('select[name="with-variants"]'); if(!this.initialFocus) { term.focus(); this.initialFocus = true; } type.val(this.model.get('type')); category.val(this.model.get('category')); minPrice.val(this.model.get('minPrice')); maxPrice.val(this.model.get('maxPrice')); condition.val(this.model.get('condition')); sort.val(this.model.get('sort')); withVariants.val(this.model.get('withVariants')); return this; }, /** * Submit the search form. * * @since 0.9 * @param {Event} e * @public */ submit(e) { e.preventDefault(); this.change(); this.model.submit(); }, /** * Load the new search parameters into the model on form change. * * @since 0.9 * @public */ change() { let term = this.$el.find('input[name="term"]'), type = this.$el.find('select[name="type"]'), category = this.$el.find('select[name="category"]'), minPrice = this.$el.find('input[name="min-price"]'), maxPrice = this.$el.find('input[name="max-price"]'), condition = this.$el.find('select[name="condition"]'), sort = this.$el.find('select[name="sort"]'), withVariants = this.$el.find('select[name="with-variants"]'); this.model.set({ 'term': term.length !== 0 ? term.val() : this.model.get('term'), 'type': type.length !== 0 ? type.val() : this.model.get('type'), 'minPrice': minPrice.length !== 0 ? minPrice.val() : this.model.get('minPrice'), 'maxPrice': maxPrice.length !== 0 ? maxPrice.val() : this.model.get('maxPrice'), 'condition': condition.length !== 0 ? condition.val() : this.model.get('condition'), 'sort': sort.length !== 0 ? sort.val() : this.model.get('sort'), 'category': category.length !== 0 ? category.val() : this.model.get('category'), 'withVariants': withVariants.length !== 0 ? withVariants.val() : this.model.get('withVariants'), }); }, }); export default SearchForm;