Skip to content

Commit c4b552c

Browse files
committed
added code and README update
1 parent 6c09c92 commit c4b552c

File tree

8 files changed

+19979
-1
lines changed

8 files changed

+19979
-1
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
11
# meteor-reactjs
2-
Meteor - ReactJS integration
2+
3+
[Meteor](http://meteor.com) smart package integrating [React](http://facebook.github.io/react/) for both client and the server, to complement or replace the default [Blaze](https://www.meteor.com/blaze) templating system.
4+
5+
## Usage
6+
7+
Inside your Meteor project, add the package:
8+
```
9+
$ meteor add hipertracker:reactjs
10+
```
11+
12+
## How it works
13+
14+
The package exposes a special `ReactMeteor.Mixin` object that can be used
15+
to enable reactive data fetching for your React components.
16+
17+
To add the `ReactMeteor.Mixin` to a React component, simply include it in
18+
the `mixins` class property:
19+
20+
```js
21+
var MyComponent = React.createClass({
22+
mixins: [ReactMeteor.Mixin],
23+
24+
// Make sure your component implements this method.
25+
getMeteorState () {
26+
return {
27+
foo: Session.get("foo"),
28+
...
29+
};
30+
}
31+
});
32+
33+
```
34+
The `getMeteorState` method should return an object of properties that
35+
will be accessed via `this.state` in the component's `render` method or
36+
elsewhere. Dependencies will be registered for any data accesses
37+
performed by `getMeteorState` so that the component can be automatically
38+
re-rendered whenever the data changes.
39+
40+
You can find some examples at [hipertracker/meteor-reactjs-examples](https://github.com/hipertracker/meteor-reactjs-examples).
41+
meteor
42+
43+
## Credits
44+
45+
The source code is based on [reactjs/react-meteor](https://github.com/reactjs/react-meteor). It has been updated to the latest Meteor and React version with enabled ES6 transforms for JSX files (--harmony).

hipertracker:reactjs-tests.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Write your tests here!
2+
// Here is an example.
3+
Tinytest.add('example', function (test) {
4+
test.equal(true, true);
5+
});

hipertracker:reactjs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Write your package code here!

package.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var meteorVersion = '1.0.3.1';
2+
var reactVersion = '0.12.2';
3+
var reactAddonsVersion = '0.9.0';
4+
5+
Package.describe({
6+
name: 'hipertracker:reactjs',
7+
summary: 'ReactJS for Meteor ',
8+
version: '0.0.2',
9+
git: 'https://github.com/hipertracker/meteor-reactjs.git'
10+
});
11+
12+
Package._transitional_registerBuildPlugin({
13+
name: 'compileJSX',
14+
use: [],
15+
sources: [
16+
'plugin/compile-jsx.js'
17+
],
18+
npmDependencies: {
19+
'react': reactVersion,
20+
'react-tools': reactVersion
21+
}
22+
});
23+
24+
25+
Package.onTest(function (api) {
26+
api.use('tinytest');
27+
api.use('hipertracker:reactjs');
28+
api.addFiles('hipertracker:reactjs-tests.js');
29+
});
30+
31+
32+
Package.on_use(function (api) {
33+
api.versionsFrom(meteorVersion);
34+
35+
// Standard distribution of React, same version as react-tools.
36+
api.add_files('vendor/react-' + reactVersion + '.js', 'client');
37+
38+
// On the server, we use the modules that ship with react.
39+
api.add_files('src/require-packages.js', 'server');
40+
api.export('React', 'server');
41+
api.export('Reflux', 'server');
42+
43+
// Meteor-enabled components should include this mixin via
44+
// React.createClass({ mixins: [ReactMeteor.Mixin], ... }).
45+
api.add_files('src/ReactMeteor.js', ['server', 'client']);
46+
api.export('ReactMeteor', ['server', 'client']);
47+
});
48+
49+
50+
Npm.depends({
51+
'react': reactVersion,
52+
'react-addons': reactAddonsVersion
53+
});

plugin/compile-jsx.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var reactTools = Npm.require('react-tools');
2+
3+
var handler = function (compileStep) {
4+
var source = compileStep.read().toString('utf8');
5+
var outputFile = compileStep.inputPath + '.js';
6+
7+
compileStep.addJavaScript({
8+
path: outputFile,
9+
sourcePath: compileStep.inputPath,
10+
data: reactTools.transform(source, {harmony: true})
11+
});
12+
};
13+
14+
Plugin.registerSourceHandler('jsx', handler);
15+

src/ReactMeteor.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var ReactMeteorMixin = {
2+
_handleMeteorChange: function () {
3+
this.setState(this.getMeteorState());
4+
},
5+
6+
_cancelComputation: function () {
7+
if (this._meteorComputation) {
8+
this._meteorComputation.stop();
9+
this._meteorComputation = null;
10+
}
11+
},
12+
13+
componentWillMount: function () {
14+
this._meteorComputation = Tracker.autorun(this._handleMeteorChange);
15+
},
16+
17+
componentWillReceiveProps: function (nextProps) {
18+
var oldProps = this.props;
19+
this.props = nextProps;
20+
this._handleMeteorChange();
21+
this.props = oldProps;
22+
},
23+
24+
componentWillUnmount: function () {
25+
this._cancelComputation();
26+
}
27+
};
28+
29+
30+
if (typeof exports === "object") {
31+
ReactMeteor = exports;
32+
} else {
33+
ReactMeteor = {};
34+
}
35+
36+
ReactMeteor.Mixin = ReactMeteorMixin;
37+

src/require-packages.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
React = Npm.require('react');
2+
React.addons = Npm.require('react-addons');

0 commit comments

Comments
 (0)