-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from legalthings/repeater_issue_with_expressions
Repeater issue with expressions
- Loading branch information
Showing
5 changed files
with
225 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Dynamic addition and removing of computed properties in current used version of Ractive (0.9.13) is not supported out of the box. | ||
* So we use this object for that purpose. It uses code, extracted from ractive, and simplified to only cover our needs | ||
* (that is only support computed properties, given as strings). | ||
*/ | ||
|
||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | ||
module.exports = RactiveDynamicComputed; | ||
} | ||
|
||
function RactiveDynamicComputed() { | ||
var self = this; | ||
|
||
this.dotRegExp = /\./g; | ||
this.computedVarRegExp = /\$\{([^\}]+)\}/g; | ||
|
||
/** | ||
* Remove computed property from existing rative instance | ||
* @param {object} ractive | ||
* @param {string} key | ||
*/ | ||
this.remove = function(ractive, key) { | ||
var escapedKey = key.replace(dotRegExp, '\\.'); | ||
|
||
delete ractive.computed[key]; | ||
delete ractive.viewmodel.computations[escapedKey]; | ||
} | ||
|
||
/** | ||
* Add computed expression to existing ractive instance | ||
* @param {object} ractive | ||
* @param {string} key | ||
* @param {string} value | ||
*/ | ||
this.add = function(ractive, key, value) { | ||
var signature = getComputationSignature(ractive, key, value); | ||
|
||
ractive.computed[key] = value; | ||
ractive.viewmodel.compute(key, signature); | ||
} | ||
|
||
function getComputationSignature(ractive, key, signature) { | ||
if (typeof signature !== 'string') { | ||
throw 'Unable to dynamically add computed property with value of type ' + (typeof signature); | ||
} | ||
|
||
var getter = createFunctionFromString(signature, ractive); | ||
var getterString = signature; | ||
|
||
return { | ||
getter: getter, | ||
setter: undefined, | ||
getterString: getterString, | ||
setterString: undefined, | ||
getterUseStack: undefined | ||
}; | ||
} | ||
|
||
function createFunctionFromString(str, bindTo) { | ||
var hasThis; | ||
|
||
var functionBody = 'return (' + str.replace(self.computedVarRegExp, function (match, keypath) { | ||
hasThis = true; | ||
return ("__ractive.get(\"" + keypath + "\")"); | ||
}) + ');'; | ||
|
||
if (hasThis) { functionBody = "var __ractive = this; " + functionBody; } | ||
var fn = new Function( functionBody ); | ||
return hasThis ? fn.bind( bindTo ) : fn; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | ||
module.exports = tmplToExpression; | ||
} | ||
|
||
var dotRegExp = /\./g; | ||
|
||
/** | ||
* Insert repeated step index into expression | ||
* @param {object} step | ||
* @param {string} expression | ||
* @return {string} | ||
*/ | ||
function tmplToExpression(expressionTmpl, group, idx) { | ||
var prefix = group + '.'; | ||
prefix = prefix.replace(dotRegExp, '\\.'); | ||
|
||
var prefixRegExp = new RegExp('\\$\\{' + prefix, 'g'); | ||
var replacement = '${' + group + '[' + idx + '].'; | ||
|
||
return expressionTmpl.replace(prefixRegExp, replacement); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
describe("test inserting repeated step index into expression", function() { | ||
var tmplToExpression = require('../../js/lib/repeated-step-expression'); | ||
|
||
function usePlaceholderProvider() { | ||
return [ | ||
{ | ||
note: 'insert indexes for "foo" step', | ||
group: 'foo', | ||
expression: '${foo.bar} + ${baz.bar} + "test" + "foo." + ${foo.zoo} + ${test.foo.zoo}', | ||
expected: '${foo[2].bar} + ${baz.bar} + "test" + "foo." + ${foo[2].zoo} + ${test.foo.zoo}' | ||
}, | ||
{ | ||
note: 'insert indexes for "foo.bar.baz" step', | ||
group: 'foo.bar.baz', | ||
expression: '${foo.bar.baz.prop1} + ${baz.bar} + "test" + "foo.bar.baz." + ${foo.bar.baz.prop2} + ${test.foo.bar.baz.zoo}', | ||
expected: '${foo.bar.baz[2].prop1} + ${baz.bar} + "test" + "foo.bar.baz." + ${foo.bar.baz[2].prop2} + ${test.foo.bar.baz.zoo}' | ||
}, | ||
{ | ||
note: 'return expression as it is if step group is not set', | ||
group: '', | ||
expression: '${foo.bar} + ${baz.bar} + "test" + "foo." + ${foo.zoo} + ${test.foo.zoo}', | ||
expected: '${foo.bar} + ${baz.bar} + "test" + "foo." + ${foo.zoo} + ${test.foo.zoo}' | ||
}, | ||
{ | ||
note: 'return expression as it is if step group is not found in expression', | ||
group: 'not_used', | ||
expression: '${foo.bar} + ${baz.bar} + "test" + "foo." + ${foo.zoo} + ${test.foo.zoo}', | ||
expected: '${foo.bar} + ${baz.bar} + "test" + "foo." + ${foo.zoo} + ${test.foo.zoo}' | ||
}, | ||
]; | ||
} | ||
|
||
usePlaceholderProvider().forEach(function(spec) { | ||
it(spec.note, function() { | ||
var result = tmplToExpression(spec.expression, spec.group, 2); | ||
|
||
expect(result).toBe(spec.expected); | ||
}); | ||
}); | ||
}); |