Change server returned newsletter body to be a json object, not a string

This commit is contained in:
Tautvidas Sipavičius
2015-12-11 17:14:23 +02:00
parent 3d82230d10
commit be3462925d
4 changed files with 23 additions and 5 deletions

View File

@ -44,10 +44,10 @@ define([
};
Module.getBody = function() {
return JSON.stringify({
return {
content: App._contentContainer.toJSON(),
globalStyles: App.getGlobalStyles().toJSON(),
});
};
};
Module.toJSON = function() {
@ -73,8 +73,7 @@ define([
});
App.on('start', function(options) {
// TODO: Other newsletter information will be needed as well.
var body = JSON.parse(options.newsletter.body);
var body = options.newsletter.body;
App._contentContainer = new (App.getBlockTypeModel('container'))(body.content, {parse: true});
App._contentContainerView = new (App.getBlockTypeView('container'))({
model: App._contentContainer,

View File

@ -72,7 +72,7 @@ define([
App.getAvailableStyles = Module.getAvailableStyles;
var body = JSON.parse(options.newsletter.body);
var body = options.newsletter.body;
this.setGlobalStyles(body.globalStyles);
});

View File

@ -15,16 +15,25 @@ class NewsletterTemplates {
if($template === false) {
wp_send_json(false);
} else {
$template->body = json_decode($template->body);
wp_send_json($template->asArray());
}
}
function getAll() {
$collection = NewsletterTemplate::findArray();
$collection = array_map(function($item) {
$item['body'] = json_decode($item['body']);
return $item;
}, $collection);
wp_send_json($collection);
}
function save($data = array()) {
if (isset($data['body'])) {
$data['body'] = json_encode($data['body']);
}
$result = NewsletterTemplate::createOrUpdate($data);
if($result !== true) {
wp_send_json($result);

View File

@ -33,6 +33,7 @@ class Newsletters {
return $segment['id'];
}, $segments);
$newsletter['options'] = $options;
$newsletter['body'] = json_decode($newsletter['body']);
wp_send_json($newsletter);
}
@ -40,6 +41,10 @@ class Newsletters {
function getAll() {
$collection = Newsletter::findArray();
$collection = array_map(function($item) {
$item['body'] = json_decode($item['body']);
return $item;
}, $collection);
wp_send_json($collection);
}
@ -54,6 +59,10 @@ class Newsletters {
unset($data['options']);
}
if (isset($data['body'])) {
$data['body'] = json_encode($data['body']);
}
$errors = array();
$result = false;
@ -282,6 +291,7 @@ class Newsletters {
}
}
}
$newsletter->body = json_decode($newsletter->body);
wp_send_json($newsletter->asArray());
}
}