encapsulating in communication component + update js tests

This commit is contained in:
Jonathan Labreuille
2016-08-08 11:36:29 +02:00
parent 1305a10ee0
commit c136d91dd2
5 changed files with 64 additions and 35 deletions

View File

@@ -51,9 +51,9 @@ define([
blocks: blocks,
}).then(_.partial(this.refreshBlocks, models));
},
refreshBlocks: function(models, response) {
refreshBlocks: function(models, renderedBlocks) {
_.each(
_.zip(models, response.data),
_.zip(models, renderedBlocks),
function(args) {
var model = args[0],
contents = args[1];
@@ -220,16 +220,16 @@ define([
var taxonomies;
var promise = CommunicationComponent.getTaxonomies(
that.model.get('contentType')
).then(function(response) {
taxonomies = response.data;
).then(function(tax) {
taxonomies = tax;
// Fetch available terms based on the list of taxonomies already fetched
var promise = CommunicationComponent.getTerms({
search: options.data.term,
taxonomies: _.keys(taxonomies)
}).then(function(response) {
}).then(function(terms) {
return {
taxonomies: taxonomies,
terms: response.data
terms: terms
};
});
return promise;

View File

@@ -108,8 +108,8 @@ define([
},
fetchAvailablePosts: function() {
var that = this;
CommunicationComponent.getPosts(this.toJSON()).done(function(response) {
that.get('_availablePosts').reset(response.data);
CommunicationComponent.getPosts(this.toJSON()).done(function(posts) {
that.get('_availablePosts').reset(posts);
that.get('_selectedPosts').reset(); // Empty out the collection
that.trigger('change:_availablePosts');
}).fail(function() {
@@ -127,8 +127,8 @@ define([
return;
}
CommunicationComponent.getTransformedPosts(data).done(function(response) {
that.get('_transformedPosts').get('blocks').reset(response.data, {parse: true});
CommunicationComponent.getTransformedPosts(data).done(function(posts) {
that.get('_transformedPosts').get('blocks').reset(posts, {parse: true});
}).fail(function() {
MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts'));
});
@@ -143,8 +143,8 @@ define([
if (data.posts.length === 0) return;
CommunicationComponent.getTransformedPosts(data).done(function(response) {
collection.add(response.data, { at: index });
CommunicationComponent.getTransformedPosts(data).done(function(posts) {
collection.add(posts, { at: index });
}).fail(function() {
MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts'));
});
@@ -305,16 +305,16 @@ define([
var taxonomies;
var promise = CommunicationComponent.getTaxonomies(
that.model.get('contentType')
).then(function(response) {
taxonomies = response.data;
).then(function(tax) {
taxonomies = tax;
// Fetch available terms based on the list of taxonomies already fetched
var promise = CommunicationComponent.getTerms({
search: options.data.term,
taxonomies: _.keys(taxonomies)
}).then(function(response) {
}).then(function(terms) {
return {
taxonomies: taxonomies,
terms: response.data
terms: terms
};
});
return promise;

View File

@@ -36,36 +36,46 @@ define([
return Module._cachedQuery({
action: 'getTaxonomies',
options: {
postType: postType,
},
postType: postType
}
}).then(function(response) {
return response.data;
});
};
Module.getTerms = function(options) {
return Module._cachedQuery({
action: 'getTerms',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getPosts = function(options) {
return Module._cachedQuery({
action: 'getPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getTransformedPosts = function(options) {
return Module._cachedQuery({
action: 'getTransformedPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getBulkTransformedPosts = function(options) {
return Module._query({
action: 'getBulkTransformedPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};