Problem serializing functions using json scxml
When using scxml-core with json object using functions the code lose all functions because is serialising the json on next line: https://gitlab.com/scion-scxml/core-base/blob/master/lib/scion-core-base.js#L174
I fixed locally with next code:
var model;
if(typeof modelOrModelFactory === 'function'){
model = initializeModelGeneratorFn(modelOrModelFactory, this.opts, this);
}else if(typeof modelOrModelFactory === 'object'){
// original code: model = JSON.parse(JSON.stringify(modelOrModelFactory)); //assume object
model = modelOrModelFactory; // remove the serialization here
}else{
throw new Error('Unexpected model type. Expected model factory function, or scjson object.');
}
The funny part is on your fiddle (which is working) the code is also different from the one on this repo: http://jsfiddle.net/jbeard4/MDkLe/11/
I see is using scion 1.1.2: https://cdn.rawgit.com/jbeard4/SCION-CORE/1.1.2/lib/scion.js
Which code is like this:
var model;
if (typeof modelOrFnGenerator === 'function') {
model = initializeModelGeneratorFn(modelOrFnGenerator, opts, this);
} else if (typeof modelOrFnGenerator === 'string') {
model = JSON.parse(modelOrFnGenerator);
} else {
model = modelOrFnGenerator;
}
You can easily reproduce with the next code:
<!DOCTYPE html>
<html lang="en">
<body>
<!-- bundle generated by rollup.js -->
<h1 id="test" style="text-align: center; color: firebrick;">This is without scxlm</h1>
<script src="js/example.js"></script>
</body>
</html>
js/example.js:
import core from '@scion-scxml/core';
var element = document.getElementById('test');
element.innerHTML = 'outside scxml!';
var statechartModel = {
id: 'loading',
onEntry: function(event){
element.innerHTML = 'with SCXML';
}
};
var interpreter = new core.Statechart(statechartModel);
interpreter.start();
Expected result is to see on the div the text "with SCXML".