Skip to content
This repository has been archived by the owner on Jul 19, 2019. It is now read-only.

Allow setting the Javascript scope for components #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion ReactJS.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class ReactJS {
* @var string
*/
$component,

/**
* The Javascript scope in which React components are contained (defaults to global)
* @var string
*/
$componentScope = 'global',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine but this should default to window. global is not defined by default.

Or perhaps even better, we don't define a default and if it's not set, then we don't try to use it.

Also nit here and other comments: ""JavaScript", not "Javascript".


/**
* Properties that go along with the component
Expand Down Expand Up @@ -80,6 +86,17 @@ function setComponent($component, $data = null) {
$this->data = json_encode($data);
return $this;
}

/**
* Sets the Javascript scope in which React components are contained
*
* @param string $scope The Javascript scope
* @return ReactJS $this instance
*/
function setComponentScope($scope) {
$this->componentScope = $scope;
return $this;
}

/**
* Custom error handler. The default one var_dumps the exception
Expand All @@ -100,7 +117,8 @@ function setErrorHandler($err) {
*/
function getMarkup() {
$js = sprintf(
"print(React.renderToString(React.createElement(%s, %s)))",
"print(React.renderToString(React.createElement(%s.%s, %s)))",
$this->componentScope,
$this->component,
$this->data);

Expand Down