diff --git a/ReactJS.php b/ReactJS.php index 89d961e..22fce20 100644 --- a/ReactJS.php +++ b/ReactJS.php @@ -21,6 +21,12 @@ class ReactJS { * @var string */ $component, + + /** + * The JavaScript scope in which React components are contained (defaults to global) + * @var string + */ + $componentScope, /** * Properties that go along with the component @@ -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 @@ -99,6 +116,10 @@ function setErrorHandler($err) { * @return string HTML string */ function getMarkup() { + if ($this->componentScope) { + $this->component = $this->componentScope.'.'.$this->component; + } + $js = sprintf( "print(React.renderToString(React.createElement(%s, %s)))", $this->component, @@ -145,7 +166,7 @@ function getJS($where, $return_var = null) { } /** - * Executes Javascript using V8JS, with primitive exception handling + * Executes JavaScript using V8JS, with primitive exception handling * * @param string $js JS code to be executed * @return string The execution response