Skip to content

Commit

Permalink
Implement JSONP output for BattleReports
Browse files Browse the repository at this point in the history
  • Loading branch information
ta2edchimp committed Jun 21, 2016
1 parent ad0d4ba commit 2b9ffcd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
25 changes: 22 additions & 3 deletions classes/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,32 @@ public static function compareVersions($firstVersion = null, $secondVersion = nu

/**
* Returns JSON as the app's response
* @param Slim $app A Slim Application instance
* @param Any $content An object of any kind to be output as JSON
* @param array $content An object of any kind to be output as JSON
*/
public static function renderJSON($app = null, $content = null) {
public static function renderJSON($content = array()) {
$app = \Slim\Slim::getInstance();
$response = $app->response;

$response['Content-Type'] = 'application/json';
$response->body(json_encode($content));
}

/**
* Returns a "padded JSON" as the app's response
* @param array $content An object of any kind to be output as JSON
* @param string $callback Name of the callback function to be called with $content
*/
public static function renderJSONP($content = array(), $callback = "") {
$app = \Slim\Slim::getInstance();
$response = $app->response;

$cbName = $callback;
if (empty($cbName)) {
$cbName = "callback";
}

$response['Content-Type'] = 'application/javascript';
$response->body("$cbName(". json_encode($content) .")");
}

}
8 changes: 6 additions & 2 deletions views/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

$battleReportDetailTitle = "Battle Overview";

$availableDetails = array("timeline", "damage", "json");
$availableDetails = array("timeline", "damage", "json", "jsonp");
if (BR_COMMENTS_ENABLED === true)
$availableDetails[] = "comments";
if (!empty($battleReportDetail)) {
Expand Down Expand Up @@ -79,7 +79,11 @@

if ($battleReportDetail === "json") {

Utils::renderJSON($app, array( "previewMeta" => $previewMeta, "battleReport" => $battleReport ));
Utils::renderJSON(array( "previewMeta" => $previewMeta, "battleReport" => $battleReport ));

} else if ($battleReportDetail === "jsonp") {

Utils::renderJSONP(array( "previewMeta" => $previewMeta, "battleReport" => $battleReport ), $app->request->get('callback'));

} else {

Expand Down

0 comments on commit 2b9ffcd

Please sign in to comment.