fix(Viewer): do not cache moddle

This may have unwanted side-effects, i.e. when using id
validation and caching.

We must ensure the meta-model element is actually instance stateless in
order to safely cache it.
This commit is contained in:
Nico Rehwaldt 2015-02-17 12:03:27 +01:00
parent 4854c96067
commit 788b143f27
3 changed files with 62 additions and 8 deletions

View File

@ -129,7 +129,7 @@ Viewer.prototype.importXML = function(xml, done) {
var self = this; var self = this;
this.moddle = this.moddle || this.createModdle(); this.moddle = this.createModdle();
this.moddle.fromXML(xml, 'bpmn:Definitions', function(err, definitions, context) { this.moddle.fromXML(xml, 'bpmn:Definitions', function(err, definitions, context) {
@ -167,8 +167,7 @@ Viewer.prototype.saveXML = function(options, done) {
}; };
Viewer.prototype.createModdle = function() { Viewer.prototype.createModdle = function() {
var options = this.options; return new BpmnModdle(this.options.moddleExtensions);
return options.moddle || new BpmnModdle(options.moddleExtensions);
}; };
Viewer.prototype.saveSVG = function(options, done) { Viewer.prototype.saveSVG = function(options, done) {

View File

@ -19,8 +19,8 @@ describe('Modeler', function() {
function createModeler(xml, done) { function createModeler(xml, done) {
var modeler = new Modeler({ container: container }); var modeler = new Modeler({ container: container });
modeler.importXML(xml, function(err) { modeler.importXML(xml, function(err, warnings) {
done(err, modeler); done(err, warnings, modeler);
}); });
} }
@ -37,13 +37,42 @@ describe('Modeler', function() {
}); });
iit('should re-import simple process', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8');
// given
createModeler(xml, function(err, warnings, modeler) {
if (err) {
return done(err);
}
// when
// mimic re-import of same diagram
modeler.importXML(xml, function(err, warnings) {
if (err) {
return done(err);
}
// then
expect(warnings.length).toBe(0);
done();
});
});
});
describe('overlay support', function() { describe('overlay support', function() {
it('should allow to add overlays', function(done) { it('should allow to add overlays', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8'); var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8');
createModeler(xml, function(err, viewer) { createModeler(xml, function(err, warnings, viewer) {
// given // given
var overlays = viewer.get('overlays'), var overlays = viewer.get('overlays'),
@ -91,7 +120,7 @@ describe('Modeler', function() {
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8'); var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8');
createModeler(xml, function(err, viewer) { createModeler(xml, function(err, warnings, viewer) {
// given // given
var bendpointMove = viewer.get('bendpointMove'), var bendpointMove = viewer.get('bendpointMove'),
@ -141,7 +170,7 @@ describe('Modeler', function() {
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8'); var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8');
createModeler(xml, function(err, modeler) { createModeler(xml, function(err, warnings, modeler) {
expect(modeler.get('bpmnjs')).toBe(modeler); expect(modeler.get('bpmnjs')).toBe(modeler);

View File

@ -42,6 +42,32 @@ describe('Viewer', function() {
}); });
it('should re-import simple process', function(done) {
var xml = fs.readFileSync('test/fixtures/bpmn/simple.bpmn', 'utf8');
// given
createViewer(xml, function(err, warnings, viewer) {
if (err) {
return done(err);
}
// when
// mimic re-import of same diagram
viewer.importXML(xml, function(err, warnings) {
// then
expect(err).toBeFalsy();
expect(warnings.length).toBe(0);
done();
});
});
});
describe('import events', function() { describe('import events', function() {
it('should fire <import.*> events', function(done) { it('should fire <import.*> events', function(done) {