Add getPosts endpoint to fetch a list of posts for search

This commit is contained in:
Tautvidas Sipavičius
2015-09-10 14:48:34 +03:00
parent 087d96838b
commit a4d1f80418
3 changed files with 70 additions and 11 deletions

View File

@ -81,15 +81,14 @@ define([
},
fetchAvailablePosts: function() {
var that = this;
// TODO: Move this logic to new AJAX query format
//mailpoet_post_wpi('posts.php', this.toJSON(), function(response) {
//console.log('Posts fetched', arguments);
//that.get('_availablePosts').reset(response);
//that.get('_selectedPosts').reset(); // Empty out the collection
//that.trigger('change:_availablePosts');
//}, function() {
//console.log('Posts fetchPosts error', arguments);
//});
WordpressComponent.getPosts(this.toJSON()).done(function(posts) {
console.log('Posts fetched', arguments);
that.get('_availablePosts').reset(posts);
that.get('_selectedPosts').reset(); // Empty out the collection
that.trigger('change:_availablePosts');
}).fail(function() {
console.log('Posts fetchPosts error', arguments);
});
},
/**
* Batch more changes during a specific time, instead of fetching

View File

@ -7,8 +7,9 @@ define([
var Module = {};
var postTypesCache,
taxonomiesCache = [],
termsCache = [];
taxonomiesCache = {},
termsCache = {},
postsCache = {};
Module.getPostTypes = function() {
if (!postTypesCache) {
@ -51,6 +52,19 @@ define([
return termsCache[key];
};
Module.getPosts = function(options) {
var key = JSON.stringify(options);
if (!postsCache[key]) {
postsCache[key] = MailPoet.Ajax.post({
endpoint: 'wordpress',
action: 'getPosts',
data: options || {},
});
}
return postsCache[key];
};
App.on('start', function(options) {
// Prefetch post types
Module.getPostTypes();