Hook up post type and term search endpoint calls for Posts block

This commit is contained in:
Tautvidas Sipavičius
2015-09-03 19:55:43 +03:00
parent befcd24164
commit bfc6c83d00

View File

@@ -16,6 +16,7 @@ define('newsletter_editor/blocks/posts', [
'backbone.marionette', 'backbone.marionette',
'backbone.radio', 'backbone.radio',
'mailpoet', 'mailpoet',
'ajax',
], function(EditorApplication, Backbone, Marionette, Radio, MailPoet) { ], function(EditorApplication, Backbone, Marionette, Radio, MailPoet) {
EditorApplication.module("blocks.posts", function(Module, App, Backbone, Marionette, $, _) { EditorApplication.module("blocks.posts", function(Module, App, Backbone, Marionette, $, _) {
@@ -255,41 +256,75 @@ define('newsletter_editor/blocks/posts', [
onRender: function() { onRender: function() {
var that = this; var that = this;
// Dynamically update available post types
App.module('components.wordpress').getPostTypes().done(_.bind(this._updateContentTypes, this));
this.$('.mailpoet_posts_categories_and_tags').select2({ this.$('.mailpoet_posts_categories_and_tags').select2({
multiple: true, multiple: true,
allowClear: true, allowClear: true,
ajax: { query: function(options) {
url: App.getConfig().get('urls.termSearch'), var taxonomies = [];
type: 'POST', // Delegate data loading to our own endpoints
dataType: 'json', EditorApplication.module('components.wordpress').getTaxonomies(that.model.get('contentType')).then(function(tax) {
delay: 250, taxonomies = tax;
data: function(searchParameter, page) { // Fetch available terms based on the list of taxonomies already fetched
return JSON.stringify({ var promise = EditorApplication.module('components.wordpress').getTerms({
postType: that.model.get('contentType'), search: options.term,
search: searchParameter, taxonomies: _.keys(taxonomies)
limit: 10, // TODO: Move this hardcoded limit to Config }).then(function(terms) {
page: page, return {
taxonomies: taxonomies,
terms: terms,
};
}); });
}, return promise;
}).done(function(args) {
// Transform taxonomies and terms into select2 compatible format
options.callback({
results: _.map(
args.terms,
function(item) {
return _.defaults({
text: args.taxonomies[item.taxonomy].labels.singular_name + ': ' + item.name,
id: item.term_id
}, item);
}
)
});
});
},
//ajax: {
//url: App.getConfig().get('urls.termSearch'),
//type: 'POST',
//dataType: 'json',
//delay: 250,
//data: function(searchParameter, page) {
//return JSON.stringify({
//postType: that.model.get('contentType'),
//search: searchParameter,
//limit: 10, // TODO: Move this hardcoded limit to Config
//page: page,
//});
//},
/** /**
* Parse results for select2. * Parse results for select2.
* Returns object, where `results` key holds a list of * Returns object, where `results` key holds a list of
* select item objects * select item objects
*/ */
results: function (data, page) { //results: function (data, page) {
return { //return {
results: _.map( //results: _.map(
data.results, //data.results,
function(item) { //function(item) {
return _.defaults({ //return _.defaults({
text: data.taxonomies[item.taxonomy].labels.singular_name + ': ' + item.name, //text: data.taxonomies[item.taxonomy].labels.singular_name + ': ' + item.name,
id: item.term_id //id: item.term_id
}, item); //}, item);
} //}
) //)
}; //};
} //}
}, //},
}).trigger( 'change' ).on({ }).trigger( 'change' ).on({
'change': function(e){ 'change': function(e){
var data = []; var data = [];
@@ -320,6 +355,19 @@ define('newsletter_editor/blocks/posts', [
changeField: function(field, event) { changeField: function(field, event) {
this.model.set(field, jQuery(event.target).val()); this.model.set(field, jQuery(event.target).val());
}, },
_updateContentTypes: function(postTypes) {
var select = this.$('.mailpoet_settings_posts_content_type'),
selectedValue = this.model.get('contentType');
select.find('option').remove();
_.each(postTypes, function(type) {
select.append(jQuery('<option>', {
value: type.name,
text: type.labels.singular_name,
}));
});
select.val(selectedValue);
},
}); });
var EmptyPostSelectionSettingsView = Marionette.ItemView.extend({ var EmptyPostSelectionSettingsView = Marionette.ItemView.extend({