Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions controllers/components/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,18 @@ public function beforeRender (&$Controller) {
$response = $this->response($data);

$this->Controller->set(compact('response'));

//if a callback function is requested, pass the callback name to the controller
//responds if following query parameters present: jsoncallback, callback
$json_callback_key = array('jsoncallback', 'callback');
foreach($json_callback_key as $key) {
if(array_key_exists($key, $this->Controller->params['url'])) {
$callback_key = $key;
}
}
if (isset($callback_key)) {
$this->Controller->set('callbackFunc', $this->Controller->params['url'][$callback_key]);
}
}

/**
Expand Down
9 changes: 7 additions & 2 deletions views/json.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ public function render ($action = null, $layout = null, $file = null) {
);
return false;
}

return $this->encode($this->viewVars['response']);
//wrap in callback function if requested
if (array_key_exists('callbackFunc', $this->viewVars)) {
return $this->viewVars['callbackFunc'].'('.$this->encode($this->viewVars['response']).')';
}
else {
return $this->encode($this->viewVars['response']);
}
}

public function encode ($response) {
Expand Down