Skip to content

Instantly share code, notes, and snippets.

@ehyland
Last active September 2, 2015 03:48
Show Gist options
  • Save ehyland/31a0501981b62eaab5a4 to your computer and use it in GitHub Desktop.
Save ehyland/31a0501981b62eaab5a4 to your computer and use it in GitHub Desktop.
React Competent in SilverStripe
var React = require('react/addons');
var App = React.createClass({
getInitialState: function() {
return {
data: window.ReactAppData
};
},
render: function() {
var data = this.state.data;
return (
<div className="event-app">
<h1>data.Title</h1>
<p>data.Content</p>
<div className="footer">data.FooterText</div>
</div>
);
}
});
<?php
class MyPage extends Page{
private static $has_one = array(
'ReactComponent' => 'MyReactComponent'
);
}
$ReactComponent
<?php
class MyReactComponent extends Dataobject{
private static $db = array(
'Title' => 'Varchar(255)',
'Content' => 'Varchar(255)',
'FooterText' => 'Varchar(255)'
);
private static $belongs_to = array(
'Parent' => 'MyPage.ReactComponent'
);
public function forTemplate(){
Requirements::javascript("path/to/app.js");
$output = "window.ReactAppData=".json_encode($this->toMap());
$output .= "<div id=\"AppRoot\"></div>";
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment