Skip to content

Commit

Permalink
fixes issue when multiple repeat instances are loaded and relevant lo…
Browse files Browse the repository at this point in the history
…gic inside the repeats is not properly evaluated (XPath caching issue), closes SEL-Columbia#507
  • Loading branch information
MartijnR committed Jul 8, 2013
1 parent 87a5cc2 commit f280ffb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Code_Igniter/application/controllers/unit_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public function generate_js_test_form_mocks()
'nested_repeats.xml',
'calcs.xml',
'readonly.xml',
'calcs_in_repeats.xml'
'calcs_in_repeats.xml',
'multiple_repeats_relevant.xml'
);
$xml_forms_path = '../devinfo/Forms/';
$save_result_path = '../js_tests/mocks/transforms.mock.js';
Expand Down
48 changes: 48 additions & 0 deletions devinfo/Forms/multiple_repeats_relevant.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>multiple default repeats with relevant questions</h:title>
<model>
<instance>
<multiple_repeats_relevant id="repeats_relevant">
<something/>
<rep jr:template="">
<val/>
<skipq/>
</rep>
<rep>
<val/>
<skipq/>
</rep>
<rep>
<val/>
<skipq/>
</rep>
<meta>
<instanceID/>
</meta>
</multiple_repeats_relevant>
</instance>
<bind nodeset="/multiple_repeats_relevant/something" relevant="1 + 1 = 2" type="string"/>
<bind nodeset="/multiple_repeats_relevant/rep/val" type="string"/>
<bind nodeset="/multiple_repeats_relevant/rep/skipq" relevant=" /multiple_repeats_relevant/rep/val = 'diarrhea'" type="int"/>
<bind calculate="concat('uuid:', uuid())" nodeset="/multiple_repeats_relevant/meta/instanceID" readonly="true()" type="string"/>
</model>
</h:head>
<h:body>
<input ref="/multiple_repeats_relevant/something">
<label>something with a relevant that will be cached</label>
</input>
<group ref="/multiple_repeats_relevant/rep">
<label></label>
<repeat nodeset="/multiple_repeats_relevant/rep">
<input ref="/multiple_repeats_relevant/rep/val">
<label>enter value</label>
</input>
<input ref="/multiple_repeats_relevant/rep/skipq">
<label>enter number</label>
</input>
</repeat>
</group>
</h:body>
</h:html>
5 changes: 5 additions & 0 deletions js_tests/mocks/transforms.mock.js

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

14 changes: 14 additions & 0 deletions js_tests/specs/form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,20 @@ describe('branching functionality', function(){
expect(dataO.node('/calcs/calc1').getVal()[0]).toEqual('');
});
});

//for some reason form.init() causes a declaration exception "Cannot read property 'style' of undefined"
//this may be a phantomjs issue, so I gave up trying to fix it.
xdescribe('inside repeats when multiple repeats are present upon loading (issue #507)', function(){
form = loadForm('multiple_repeats_relevant.xml');
form.init();
var $relNodes = form.getFormO().$.find('[name="/multiple_repeats_relevant/rep/skipq"]').parent('.jr-branch');
it ('correctly evaluates the relevant logic of each question inside all repeats', function(){
expect($relNodes.length).toEqual(2);
//check if both questions with 'relevant' attributes in the 2 repeats are disabled
expect($relNodes.eq(0).hasClass('disabled')).toBe(true);
expect($relNodes.eq(1).hasClass('disabled')).toBe(true);
});
});
});

describe('Required field validation', function(){
Expand Down
7 changes: 3 additions & 4 deletions src/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -1522,8 +1522,7 @@ function Form (formSelector, dataStr, dataStrToEdit){
* @return {?boolean} [description]
*/
this.update = function(changedNodeNames){
var i, p, $branchNode, result, namesArr, cleverSelector, insideRepeat, insideRepeatClone,
cacheIndex = null,
var i, p, $branchNode, result, namesArr, cleverSelector, insideRepeat, insideRepeatClone, cacheIndex,
relevantCache = {},
alreadyCovered = [],
that = this,
Expand All @@ -1547,6 +1546,7 @@ function Form (formSelector, dataStr, dataStrToEdit){
return;
}
p = {};
cacheIndex = null;

p.relevant = $(this).attr('data-relevant');
p.path = parent.input.getName($(this));
Expand Down Expand Up @@ -1592,7 +1592,6 @@ function Form (formSelector, dataStr, dataStrToEdit){
//cacheIndex = p.relevant+'__'+p.path+'__'+p.ind;
}
}

if (cacheIndex && typeof relevantCache[cacheIndex] !== 'undefined'){
result = relevantCache[cacheIndex];
}
Expand Down Expand Up @@ -2773,7 +2772,7 @@ function Form (formSelector, dataStr, dataStrToEdit){
$clone.insertAfter($node)
.parent('.jr-group').numberRepeats();

//if not done asynchronously, this code causes a style undefined exception in Jasmine unit tests with jQuery 1.9 and 2.0
//if not done asynchronously, this code causes a "style undefined" exception in Jasmine unit tests with jQuery 1.9 and 2.0
//but this breaks loading of default values inside repeats
//this is caused by show() not being able to find the 'property "style" of undefined'
//setTimeout(function(){
Expand Down

0 comments on commit f280ffb

Please sign in to comment.