Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add isRestore parameter to setState #20

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oat-sa/tao-item-runner",
"version": "0.4.0",
"version": "0.5.0",
"displayName": "TAO Item Runner",
"description": "TAO Item Runner modules",
"files": [
Expand Down
36 changes: 18 additions & 18 deletions src/runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ It works in 2 steps:
2. Create an `ItemRunner` instance for each item to render.

```
+--------------------------------------------------+ +----------------------------------------------+
| ItemRunner | | Provider |
|--------------------------------------------------| |----------------------------------------------|
| <construct>(Object itemData) : ItemRunner | | |
| | delegates | |
| init() : ItemRunner +-------------------> init(Object data, Func done) : void |
| render(HTMLElement elt) : ItemRunner +-------------------> render(HTMLElement elt, Func done) : void |
| getState() : Object +-------------------> getState() : Object |
| setState(Object state) : ItemRunner +-------------------> setState(Object state) : void |
| getResponses() : Array +-------------------> getResponses() : Array |
| clear() : ItemRunner +-------------------> clear() : void |
| getData() : Object +-------------------> getData() : Object |
| | | |
| on(event,Func handler) : ItemRunner | | |
| off(event) : ItemRunner | | |
| trigger(event) : ItemRunner | | |
+--------------------------------------------------+ +----------------------------------------------+
+--------------------------------------------------------------------+ +-----------------------------------------------------+
| ItemRunner | | Provider |
|--------------------------------------------------------------------| |-----------------------------------------------------|
| <construct>(Object itemData) : ItemRunner | | |
| | delegates | |
| init() : ItemRunner +-------------------> init(Object data, Func done) : void |
| render(HTMLElement elt) : ItemRunner +-------------------> render(HTMLElement elt, Func done) : void |
| getState() : Object +-------------------> getState() : Object |
| setState(Object state, boolean isInitial) : ItemRunner +-------------------> setState(Object state, boolean isInitial) : void |
| getResponses() : Array +-------------------> getResponses() : Array |
| clear() : ItemRunner +-------------------> clear() : void |
| getData() : Object +-------------------> getData() : Object |
| | | |
| on(event,Func handler) : ItemRunner | | |
| off(event) : ItemRunner | | |
| trigger(event) : ItemRunner | | |
+--------------------------------------------------------------------+ +-----------------------------------------------------+
```

## Sample
Expand Down Expand Up @@ -112,7 +112,7 @@ define(['itemRunner'], function(itemRunner){
//oh something has changed in the item, you can store the state.
})

.setState(initialState)
.setState(initialState, isInitialStateRestore)

.init(); //let's start
});
Expand Down
9 changes: 5 additions & 4 deletions src/runner/api/itemRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ var itemRunnerFactory = function itemRunnerFactory(providerName, data, options)

//set item state to restore item state after rendering if the provider enables it
if (options.state) {
this.setState(options.state);
this.setState(options.state, true);
}

if (flow.init.done === false) {
Expand Down Expand Up @@ -360,11 +360,12 @@ var itemRunnerFactory = function itemRunnerFactory(providerName, data, options)
* This should have the effect to restore the item state.
*
* @param {Object} state - the new state
* @param {boolean} [isInitialStateRestore] - state restoring or not
* @returns {itemRunner}
*
* @fires itemRunner#error if the state type doesn't match
*/
setState: function(state) {
setState: function(state, isInitialStateRestore = false) {
if (!_.isPlainObject(state)) {
return this.trigger(
'error',
Expand All @@ -375,7 +376,7 @@ var itemRunnerFactory = function itemRunnerFactory(providerName, data, options)
//the state will be applied only when the rendering is made
if (flow.render.done === false) {
flow.render.pending.push(function() {
this.setState(state);
this.setState(state, isInitialStateRestore);
});
} else {
if (_.isFunction(provider.setState)) {
Expand All @@ -384,7 +385,7 @@ var itemRunnerFactory = function itemRunnerFactory(providerName, data, options)
* @callback SetStateItemProvider
* @param {Object} state - the state to set
*/
provider.setState.call(this, state);
provider.setState.call(this, state, isInitialStateRestore);
}
}
return this;
Expand Down
19 changes: 12 additions & 7 deletions test/runner/api/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,21 @@ define(['jquery', 'lodash', 'taoItems/runner/api/itemRunner', 'test/taoItems/run

QUnit.test('set initial state', function(assert) {
var ready = assert.async();
assert.expect(3);
assert.expect(4);

var $container = $('#item-container');
const $container = $('#item-container');
assert.equal($container.length, 1, 'the item container exists');

itemRunner.register('dummyProvider', dummyProvider);
const originalSetState = dummyProvider.setState;
itemRunner.register('dummyProvider', Object.assign({}, dummyProvider, {
setState: function(newState, isInitialSetStateRequest) {
assert.equal(isInitialSetStateRequest, true, 'initial set state request is correct');
originalSetState.call(this, newState, isInitialSetStateRequest);
}
}));

var runner = itemRunner('dummyProvider', {
type: 'number'
itemRunner('dummyProvider', {
type: 'number',
})
.on('render', function() {
var $input = $('input', $container);
Expand All @@ -460,8 +466,7 @@ define(['jquery', 'lodash', 'taoItems/runner/api/itemRunner', 'test/taoItems/run
ready();
})
.init()
.setState({ value: 13 })
.render($container);
.render($container, { state: { value: 13 }});
});

QUnit.test('set a wrong state', function(assert) {
Expand Down