Skip to content

Commit e87d5c0

Browse files
committed
Added blocks.executeExpression().
closes #154.
1 parent 67c32c7 commit e87d5c0

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/query/Expression.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ define([
88
var Expression = {
99
Html: 0,
1010
ValueOnly: 2,
11+
Raw: 3,
1112
NodeWise: 4,
1213

1314
Create: function (text, attributeName, element) {
@@ -70,7 +71,7 @@ define([
7071

7172
GetValue: function (context, elementData, expression, type) {
7273
var nodeWise = type == Expression.NodeWise;
73-
var value = nodeWise ? [] : '';
74+
var value = nodeWise || type == Expression.Raw ? [] : '';
7475
var length = expression.length;
7576
var index = -1;
7677
var chunk;
@@ -118,6 +119,8 @@ define([
118119

119120
if (nodeWise) {
120121
value[expression.nodeLength - 1] = (value[expression.nodeLength - 1] || '') + tempValue;
122+
} else if (type == Expression.Raw) {
123+
value.push(tempValue);
121124
} else {
122125
value += tempValue;
123126
}
@@ -163,6 +166,10 @@ define([
163166

164167
observables = Observer.stopObserving();
165168

169+
if (type == Expression.Raw) {
170+
return {observables: observables, result: result, value: value};
171+
}
172+
166173
if (type != Expression.ValueOnly && type != Expression.NodeWise && (isObservable || observables.length)) {
167174
if (!attributeName) {
168175
elementData = ElementsData.createIfNotExists();

src/query/methods.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ define([
66
'./DomQuery',
77
'./VirtualElement',
88
'./ElementsData',
9-
'./serverData'
10-
], function (blocks, dataQueryAttr, OBSERVABLE, createVirtual, DomQuery, VirtualElement, ElementsData, serverData) {
9+
'./serverData',
10+
'./Expression'
11+
], function (blocks, dataQueryAttr, OBSERVABLE, createVirtual, DomQuery, VirtualElement, ElementsData, serverData, Expression) {
1112
/**
1213
* Performs a query operation on the DOM. Executes all data-query attributes
1314
* and renders the html result to the specified HTMLElement if not specified
@@ -175,4 +176,56 @@ define([
175176
}
176177
return null;
177178
};
179+
180+
/**
181+
* Executes expressions on a specified context.
182+
*
183+
* @param {string} expression The expression to execute.
184+
* @param {Object} context The context to exute the expression on.
185+
* The context for an element can be get via block.context().
186+
* @param {Object} [options] An optional options objecz.
187+
* @param {bool} [options.raw] If true the function returns an array with the raw value. Default: false
188+
*
189+
* @returns {string|Object[]}] Returns the result of the expressions as a string.
190+
* Or if options.raw is specified it returns an array of objects (see second example for details);
191+
* @example {javascript}
192+
* var context = {greeter: blocks.observable("world")};
193+
* var expression = "Hello {{greeter}}!";
194+
* blocks.executeExpression(expression, context);
195+
* // -> "Hello world!"
196+
*
197+
* blocks.executeExpression(expression, context, {raw: true});
198+
* // -> [{observables: [], value: "Hello ", result: "Hello "},
199+
* // {observables: [observable<"world">], value: observable<"world">, result: "world"},
200+
* // {observable: [], value: "!", result: "!"}]
201+
*/
202+
blocks.executeExpression = function (expression, context, options) {
203+
options = options || {};
204+
var expressionData = Expression.Create(blocks.unwrapObservable(expression));
205+
var setContext = false;
206+
var value;
207+
var values = [];
208+
context = blocks.unwrapObservable(context);
209+
210+
if (!blocks.isObject(context)) {
211+
context = {$this: context};
212+
} else if (!context.$this) {
213+
setContext = true;
214+
context.$this = context;
215+
}
216+
217+
value = Expression.GetValue(context, null, expressionData, options.raw ? Expression.Raw : Expression.ValueOnly);
218+
219+
if (setContext) {
220+
context.$this = undefined;
221+
}
222+
223+
if (options.raw) {
224+
blocks.each(blocks.toArray(value), function (val, i) {
225+
values[i] = blocks.isObject(val) ? val : {observables: [], result: val, value: val};
226+
})
227+
}
228+
229+
return options.raw ? values : value;
230+
};
178231
});

0 commit comments

Comments
 (0)