Add caching endpoint calls for term search

This commit is contained in:
Tautvidas Sipavičius
2015-09-03 15:05:47 +03:00
parent be6adfc158
commit e8badf8beb
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,63 @@
define('newsletter_editor/components/wordpress', [
'newsletter_editor/App',
'backbone',
'backbone.marionette',
'mailpoet',
'ajax'
], function(EditorApplication, Backbone, Marionette, MailPoet) {
EditorApplication.module("components.wordpress", function(Module, App, Backbone, Marionette, $, _) {
"use strict";
var postTypesCache,
taxonomiesCache = [],
termsCache = [];
Module.getPostTypes = function() {
if (!postTypesCache) {
postTypesCache = MailPoet.Ajax.post({
endpoint: 'wordpress',
action: 'getPostTypes',
data: {},
}).then(function(types) {
return _.values(types);
});
}
return postTypesCache;
};
Module.getTaxonomies = function(postType) {
if (!taxonomiesCache[postType]) {
taxonomiesCache[postType] = MailPoet.Ajax.post({
endpoint: 'wordpress',
action: 'getTaxonomies',
data: {
postType: postType,
},
});
}
return taxonomiesCache[postType];
};
Module.getTerms = function(taxonomies) {
if (!termsCache[taxonomies]) {
termsCache[taxonomies] = MailPoet.Ajax.post({
endpoint: 'wordpress',
action: 'getTerms',
data: {
taxonomies: taxonomies,
},
});
}
return termsCache[taxonomies];
};
App.on('start', function(options) {
// Prefetch post types
Module.getPostTypes();
});
});
});

View File

@ -84,6 +84,7 @@ config.push(_.extend({}, baseConfig, {
'newsletter_editor/components/content.js',
'newsletter_editor/components/heading.js',
'newsletter_editor/components/save.js',
'newsletter_editor/components/wordpress.js',
'newsletter_editor/behaviors/BehaviorsLookup.js',
'newsletter_editor/behaviors/ColorPickerBehavior.js',
'newsletter_editor/behaviors/ContainerDropZoneBehavior.js',