Skip to content

Commit

Permalink
Fixed a minor bug which would break the simple templating under certa…
Browse files Browse the repository at this point in the history
…in circumstances.
  • Loading branch information
John Louderback committed May 8, 2014
1 parent c199c0d commit 0f91dde
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions examples/data-template-usage3.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ <h2>GDB Data Template Example 3</h2>
</div>
</div>
<h3 data-watching="person.name, person.donation.wholeDollar, person.donation.cents" data-gdb-template="<<1>> would like to donate $<<2>>.<<3>>"></h3>
<div class="form-group">
<label for="sentence">Sentence</label>
<input id="sentence" data-watching="person.name, person.donation.wholeDollar, person.donation.cents" data-gdb-template="<<1>> would like to donate $<<2>>.<<3>>" type="text" class="form-control">
</div>
</div>
Edit the text above!
<h2>GDB Template</h2>
Expand Down
17 changes: 12 additions & 5 deletions jquery.gdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,24 +212,31 @@

if ($this.is("[" + options.dataWatchingAttr + "][" + options.dataTemplateAttr + "]")) {//If this element is watching locations in the model and has a gdb template
var splitBys=$(this).attr(options.dataTemplateAttr).split(new RegExp(GDB.helpers.escapeForRegEx(options.templateOpeningDelimiter)+"\\d+"+GDB.helpers.escapeForRegEx(options.templateClosingDelimiter),"g"));//Create an array of indexes which will be used in a regular expression to decipher where template information is stored
splitBys.forEach(function(item, i){//loop through split bys
if(item==="")
splitBys.splice(i,1);
var toDelete=[];
splitBys.forEach(function(item,i){//loop through split bys
if(item.length===0)
toDelete.push(i);
else
splitBys[i]=GDB.helpers.escapeForRegEx(item);//escape value for regex usage.
});
var subtract=0;
toDelete.forEach(function(index){
splitBys.splice(parseInt(index)-subtract, 1);
subtract++;
});

var modelLocationValuesArray=[];//An array for mapping data in the template to locations in the model
var valueArray=rawValue.split(new RegExp(splitBys.join("|"), "g"));
var indexArray=$(this).attr(options.dataTemplateAttr).split(new RegExp(splitBys.join("|"), "g"));
var modelLocations=$(this).attr(options.dataWatchingAttr).split(",");

indexArray.forEach(function(value,i){
var index=(value.replace(new RegExp(GDB.helpers.escapeForRegEx(options.templateOpeningDelimiter)+"|"+GDB.helpers.escapeForRegEx(options.templateClosingDelimiter),"g"), "")-1);
var index=(parseInt(value.replace(new RegExp(GDB.helpers.escapeForRegEx(options.templateOpeningDelimiter)+"|"+GDB.helpers.escapeForRegEx(options.templateClosingDelimiter),"g"), ""))-1);
modelLocationValuesArray.push({
location: modelLocations[index].trim(),
value: typeof valueArray[i] === "undefined" ? '""' : JSON.stringify(valueArray[i])
});
});
console.log(splitBys);
modelLocationValuesArray.forEach(function(modelLocationAndValue){
console.log("modelsToMonitor." + modelLocationAndValue.location + "=" + modelLocationAndValue.value);
eval("modelsToMonitor." + modelLocationAndValue.location + "=" + modelLocationAndValue.value);//evaluate the path in the model to which the data is bound.
Expand Down
Loading

0 comments on commit 0f91dde

Please sign in to comment.