Skip to content

Commit

Permalink
Merge pull request #61 from TomCaserta/react-fix
Browse files Browse the repository at this point in the history
Fixes script race condition, updates react to latest and fixes travis tests
  • Loading branch information
talyssonoc committed Jan 20, 2016
2 parents 313056f + 0c98d64 commit 0b263a4
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
composer.lock
node_modules/
package.json
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ before_install:
- sudo apt-get update
- sudo apt-get install libv8-dev g++ cpp valgrind libxml2-dev -y
- printf "\n" | pecl install v8js-0.1.3
- phpenv config-add travis.php.ini
- travis_retry composer self-update

install:
- travis_retry composer install --no-interaction --prefer-source

script: valgrind --trace-children=yes vendor/bin/phpunit --debug --verbose
script: valgrind --trace-children=yes vendor/bin/phpunit --debug --verbose
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The `@react_component` directive accepts 3 arguments:

//example
@react_component('Message', [ 'title' => 'Hello, World' ], [ 'prerender' => true ])

// example using namespaced component
@react_component('Acme.Message', [ 'title' => 'Hello, World' ], [ 'prerender' => true ])
```
Expand All @@ -71,7 +71,7 @@ The `@react_component` directive accepts 3 arguments:

All your components should be inside `public/js/components.js` (you can configure it, see below) and be global.

You must include `react.js` and `react_ujs.js` (in this order) in your view.
You must include `react.js`, `react-dom.js` and `react_ujs.js` (in this order) in your view. You can concatenate these files together using laravel-elixir.

`react-laravel` provides a ReactJS installation and the `react_us.js` file, they'll be at `public/vendor/react-laravel` folder after you install `react-laravel` and run:

Expand All @@ -83,6 +83,7 @@ For using the files provided by `react-laravel` and your `components.js` file, a

```html
<script src="{{ asset('vendor/react-laravel/react.js') }}"></script>
<script src="{{ asset('vendor/react-laravel/react-dom.js') }}"></script>
<script src="{{ asset('js/components.js') }}"></script>
<script src="{{ asset('vendor/react-laravel/react_ujs.js') }}"></script>
```
Expand All @@ -96,13 +97,17 @@ You can change settings to `react-laravel` at the `config/react.php` file:
```php
return [
'source' => 'path_for_react.js',
'dom-source' => 'path_for_react-dom.js',
'dom-server-source' => 'path_for_react-dom-server.js',
'components' => 'path_for_file_containing_your_components.js'
];
```

Both of then are optional.
All of them are optional.

* `source`: defaults to `public/vendor/react/laravel/react.js`.
* `source`: defaults to `public/vendor/react-laravel/react.js`.
* `dom-source`: defaults to `public/vendor/react-laravel/react-dom.js`.
* `dom-server-source`: defaults to `public/vendor/react-laravel/react-dom-server.js`.
* `components`: defaults to `public/js/components.js`

Your `components.js` file should also be included at your view, and all your components must be at the `window` object.
Expand Down
11 changes: 8 additions & 3 deletions assets/react_ujs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,25 @@
reactClass = element.getAttribute(ReactLaravelUJS.CLASS_NAME_ATTR).split('.').reduce(index, window);
props = JSON.parse(element.getAttribute(ReactLaravelUJS.PROPS_ATTR));

React.render(React.createElement(reactClass, props), element);
ReactDOM.render(React.createElement(reactClass, props), element);
}
},

unmountComponents: function unmountComponents() {
var elements = ReactLaravelUJS.getElements();

for(var i = 0; i < elements.length; i++) {
React.unmountComponentAtNode(elements[i]);
ReactDOM.unmountComponentAtNode(elements[i]);
}
},

handleEvents: function handleEvents() {
document.addEventListener('DOMContentLoaded', ReactLaravelUJS.mountComponents);
if (document.readyState == "complete" || document.readyState == "loaded" || document.readyState == "interactive") {
ReactLaravelUJS.mountComponents();
}
else {
document.addEventListener('DOMContentLoaded', ReactLaravelUJS.mountComponents);
}
window.addEventListener('unload', ReactLaravelUJS.unmountComponents);
}
};
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
},
"extra": {
"require-npm": {
"react": "^0.13.3"
"react": "^0.14.6",
"react-dom": "^0.14.6"
}
},
"authors": [
Expand Down
4 changes: 3 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

return [
'source' => public_path('vendor/react-laravel/react.js'),
'components' => public_path('js/components.js'),
'dom-source' => public_path('vendor/react-laravel/react-dom.js'),
'dom-server-source' => public_path('vendor/react-laravel/react-dom-server.js'),
'components' => public_path('js/components.js')
];
8 changes: 7 additions & 1 deletion lib/ReactServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function boot() {
$this->publishes([
$prev . 'assets' => public_path('vendor/react-laravel'),
$prev . 'node_modules/react/dist' => public_path('vendor/react-laravel'),
$prev . 'node_modules/react-dom/dist' => public_path('vendor/react-laravel'),
], 'assets');

$this->publishes([
Expand All @@ -44,8 +45,13 @@ public function register() {

$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'react');

$reactSource = file_get_contents(config('react.source'));
$reactBaseSource = file_get_contents(config('react.source'));
$reactDomSource = file_get_contents(config('react.dom-source'));
$reactDomServerSource = file_get_contents(config('react.dom-server-source'));
$componentsSource = file_get_contents(config('react.components'));
$reactSource = $reactBaseSource;
$reactSource .= $reactDomSource;
$reactSource .= $reactDomServerSource;

if(App::environment('production')) {
Cache::forever('reactSource', $reactSource);
Expand Down
2 changes: 2 additions & 0 deletions phpunit.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
require_once 'vendor/autoload.php';

$GLOBALS['reactSource'] = file_get_contents(__DIR__ . '/node_modules/react/dist/react.js');
$GLOBALS['reactSource'] .= file_get_contents(__DIR__ . '/node_modules/react-dom/dist/react-dom.js');
$GLOBALS['reactSource'] .= file_get_contents(__DIR__ . '/node_modules/react-dom/dist/react-dom-server.js');
$GLOBALS['componentsSource'] = file_get_contents(__DIR__ . '/tests/fixtures/components.js');

class TestHelpers {
Expand Down
6 changes: 3 additions & 3 deletions tests/PrerenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use React\React;

class PrerenderTagTest extends PHPUnit_Framework_TestCase {

protected $react;

public function setUp() {
Expand All @@ -25,9 +25,9 @@ public function testWithPrerender() {

$expectedElement = TestHelpers::stringToElement('<div><span>Hello, </span><strong>react-laravel</strong></div>');
$elementWithoutAttributes = TestHelpers::removeAttributes($wrapperElement->childNodes->item(0));

$this->assertEquals(TestHelpers::innerHTML($expectedElement),
TestHelpers::innerHTML($elementWithoutAttributes));
}

}
}
2 changes: 1 addition & 1 deletion tests/fixtures/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var Alert = React.createClass({displayName: 'Alert',
React.createElement('strong', null, this.props.name)
);
}
});
});
1 change: 1 addition & 0 deletions travis.php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extension=v8js.so

0 comments on commit 0b263a4

Please sign in to comment.