|
6 | 6 | './DomQuery',
|
7 | 7 | './VirtualElement',
|
8 | 8 | './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) { |
11 | 12 | /**
|
12 | 13 | * Performs a query operation on the DOM. Executes all data-query attributes
|
13 | 14 | * and renders the html result to the specified HTMLElement if not specified
|
@@ -175,4 +176,56 @@ define([
|
175 | 176 | }
|
176 | 177 | return null;
|
177 | 178 | };
|
| 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 | + }; |
178 | 231 | });
|
0 commit comments