Skip to content

Commit 1d9d2d7

Browse files
committed
rollback breaking change, add doc for the change
1 parent dea7f24 commit 1d9d2d7

File tree

7 files changed

+45
-11
lines changed

7 files changed

+45
-11
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ var I13nDempApp = setupI13n(DemoApp, {
8686

8787
Or follow our guide and [create your own](./docs/guides/createPlugins.md).
8888

89-
9089
## I13n Tree
9190
![I13n Tree](https://cloud.githubusercontent.com/assets/3829183/7980892/0b38eb70-0a60-11e5-8cc2-712ec42089fc.png)
9291

docs/api/setupI13n.md

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ var I13nDempApp = setupI13n(DemoApp, {
2727
}, [someReactI13nPlugin]);
2828

2929
// then you could use I13nDemoApp to render you app
30+
31+
### Create and access the ReactI13n instance
32+
33+
What we do with `setupI13n` is that we will create the `ReactI13n` instance, along with a root node of the I13nTree, passing them via component context to the children.
34+
35+
It's designed to work within React components, you should be able to just [utilFuctions](https://github.com/yahoo/react-i13n/blob/master/docs/guides/utilFunctions.md) and trigger i13n events. In case you want to do this out of React components, you can access `window._reactI13nInstance` directly.
36+
37+
If you have multiple react trees in one page, we will create multiple i13n trees based on how many react tree you have. On client side the [utilFuctions](https://github.com/yahoo/react-i13n/blob/master/docs/guides/utilFunctions.md) still work based on the global instance object, while on server side, only the children under `setupI13n` can get the react i13n instance as we don't have a proper way to share the reactI13n instance without causing [memory leak](https://github.com/yahoo/react-i13n/pull/100).
38+
3039
```
3140
3241
### Util Functions

src/libs/ReactI13n.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ if ('client' === ENVIRONMENT) {
2323
* @param {Object} options options object
2424
* @param {Boolean} options.isViewportEnabled if enable viewport checking
2525
* @param {Object} options.rootModelData model data of root i13n node
26+
* @param {Object} options.i13nNodeClass the i13nNode class, you can inherit it with your own functionalities
2627
* @param {String} options.scrollableContainerId id of the scrollable element that your components
2728
* reside within. Normally, you won't need to provide a value for this. This is only to
2829
* support viewport checking when your components are contained within a scrollable element.
@@ -32,6 +33,7 @@ if ('client' === ENVIRONMENT) {
3233
var ReactI13n = function ReactI13n (options) {
3334
debug('init', options);
3435
options = options || {};
36+
this._i13nNodeClass = 'function' === typeof options.i13nNodeClass ? options.i13nNodeClass : I13nNode;
3537
this._plugins = {};
3638
this._eventsQueues = {};
3739
this._isViewportEnabled = options.isViewportEnabled || false;
@@ -45,7 +47,8 @@ var ReactI13n = function ReactI13n (options) {
4547
* @method createRootI13nNode
4648
*/
4749
ReactI13n.prototype.createRootI13nNode = function createRootI13nNode () {
48-
this._rootI13nNode = new I13nNode(null, this._rootModelData, false);
50+
var I13nNodeClass = this.getI13nNodeClass();
51+
this._rootI13nNode = new I13nNodeClass(null, this._rootModelData, false);
4952
if ('client' === ENVIRONMENT) {
5053
this._rootI13nNode.setDOMNode(document.body);
5154
}
@@ -128,6 +131,15 @@ ReactI13n.prototype.getEventHandlers = function getEventHandlers (eventName, pay
128131
return promiseHandlers;
129132
};
130133

134+
/**
135+
* Get I13n node class
136+
* @method getI13nNodeClass
137+
* @return {Object} I13nNode class
138+
*/
139+
ReactI13n.prototype.getI13nNodeClass = function getI13nNodeClass () {
140+
return this._i13nNodeClass;
141+
};
142+
131143
/**
132144
* Get isViewportEnabled value
133145
* @method isViewportEnabled
@@ -175,6 +187,7 @@ ReactI13n.prototype.getRootI13nNode = function getRootI13nNode () {
175187
ReactI13n.prototype.updateOptions = function updateOptions (options) {
176188
debug('updated', options);
177189
options = options || {};
190+
this._i13nNodeClass = 'function' === typeof options.i13nNodeClass ? options.i13nNodeClass : this._i13nNodeClass;
178191
this._isViewportEnabled = (undefined !== options.isViewportEnabled) ?
179192
options.isViewportEnabled : this._isViewportEnabled;
180193
this._rootModelData = options.rootModelData ? options.rootModelData : this._rootModelData;

src/mixins/I13nMixin.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,9 @@ var I13nMixin = {
366366
var self = this;
367367
var parentI13nNode = self._getParentI13nNode();
368368
var reactI13n = self._getReactI13n();
369+
var I13nNodeClass = (reactI13n && reactI13n.getI13nNodeClass()) || I13nNode;;
369370
// TODO @kaesonho remove BC for model
370-
self._i13nNode = new I13nNode(
371+
self._i13nNode = new I13nNodeClass(
371372
parentI13nNode,
372373
self.props.i13nModel || self.props.model,
373374
self.isLeafNode(),

src/mixins/I13nUtils.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var I13nUtils = {
1414
executeEvent: React.PropTypes.func,
1515
getI13nNode: React.PropTypes.func,
1616
parentI13nNode: React.PropTypes.object,
17-
reactI13nInstance: React.PropTypes.object
17+
_reactI13nInstance: React.PropTypes.object
1818
})
1919
},
2020

@@ -23,7 +23,7 @@ var I13nUtils = {
2323
executeEvent: React.PropTypes.func,
2424
getI13nNode: React.PropTypes.func,
2525
parentI13nNode: React.PropTypes.object,
26-
reactI13nInstance: React.PropTypes.object
26+
_reactI13nInstance: React.PropTypes.object
2727
})
2828
},
2929

@@ -34,12 +34,12 @@ var I13nUtils = {
3434
*/
3535
getChildContext: function () {
3636
return {
37-
i13n: Object.assign({}, this.context.i13n, {
37+
i13n: {
3838
executeEvent: this.executeI13nEvent,
3939
getI13nNode: this.getI13nNode,
4040
parentI13nNode: this._i13nNode,
41-
reactI13nInstance: this._getReactI13n()
42-
})
41+
_reactI13nInstance: this._getReactI13n()
42+
}
4343
};
4444
},
4545

@@ -53,11 +53,23 @@ var I13nUtils = {
5353
*/
5454
executeI13nEvent: function (eventName, payload, callback) {
5555
var reactI13nInstance = this._getReactI13n();
56+
var errorMessage = '';
5657
payload = payload || {};
5758
payload.i13nNode = payload.i13nNode || this.getI13nNode();
5859
if (reactI13nInstance) {
5960
reactI13nInstance.execute(eventName, payload, callback);
6061
} else {
62+
/*if ('production' !== process.env.NODE_ENV) {
63+
errorMessage = 'ReactI13n instance is not found, ' +
64+
'please make sure you have setupI13n on the root component. ';
65+
if (!IS_CLIENT) {
66+
errorMessage += 'On server side, ' +
67+
'you can only execute the i13n event on the components under setupI13n, ' +
68+
'please make sure you are calling executeI13nEvent correctly';
69+
}
70+
console && console.warn && console.warn(errorMessage);
71+
console && console.trace && console.trace();
72+
}*/
6173
callback && callback();
6274
}
6375
},
@@ -83,7 +95,7 @@ var I13nUtils = {
8395
globalReactI13n = window._reactI13nInstance;
8496
}
8597
return this._reactI13nInstance ||
86-
(this.context && this.context.i13n && this.context.i13n.reactI13nInstance) ||
98+
(this.context && this.context.i13n && this.context.i13n._reactI13nInstance) ||
8799
globalReactI13n;
88100
},
89101

src/utils/setupI13n.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = function setupI13n (Component, options, plugins) {
3030

3131
mixins: [I13nUtils],
3232

33-
displayName: options.displayName || ('setupI13n(' + componentName + ')'),
33+
displayName: options.displayName || ('RootI13n' + componentName),
3434

3535
autobind: false,
3636

tests/unit/utils/setupI13n.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('setupI13n', function () {
7070

7171
// check the initial state is correct after render
7272
var I13nTestApp = setupI13n(TestApp, mockData.options, [mockData.plugin]);
73-
expect(I13nTestApp.displayName).to.eql('setupI13n(TestApp)');
73+
expect(I13nTestApp.displayName).to.eql('RootI13nTestApp');
7474
var container = document.createElement('div');
7575
var component = ReactDOM.render(React.createElement(I13nTestApp, {}), container);
7676
expect(mockData.reactI13n._options).to.eql(mockData.options);

0 commit comments

Comments
 (0)