Polymer Expression evaluation via Javascript

160 views
Skip to first unread message

ashley....@gmail.com

unread,
Feb 2, 2015, 11:21:03 AM2/2/15
to polym...@googlegroups.com
Good Day All,

I am building an API that needs to evaluate expressions outside of the normal web view (configuration file). I was hoing to use the polymer-expression code to manually evaluate expressions defined in a JSON configuration file and apply a model / context. I have got a sample working as long as I do not add any context variables to it

By doing
...

<script src="/javascript/esprima.js"></script>
<script src="/javascript/polymer-expressions.js"></script>

var myExpression = PolymerExpressions.getExpression("2 * 3 + 2");
var result = myExpression.expression();

...

However how would I go about passing a model/context so I can evaluate against variables using this manual process?, Not sure what I am doing wrong any help pointing me in the right direction would be appreciated

var model = { X: 1 };
var myExpression = PolymerExpressions.getExpression("2 * 3 + X");
var result = myExpression.expression(model);

Thanks

Ashley


Arthur Evans

unread,
Feb 3, 2015, 3:06:40 PM2/3/15
to ashley....@gmail.com, polymer-dev
I think you want getValue:

var myExpression = PolymerExpressions.getExpression("2 + 2 * 5");

var result = myExpression.getValue({ x: 5 }, undefined, {});

You'll also need observe-js, which defines the Path object:



Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/fc02130e-4c15-48aa-a35a-b803e84b8660%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ashley....@gmail.com

unread,
Feb 4, 2015, 9:58:39 PM2/4/15
to polym...@googlegroups.com, ashley....@gmail.com
Thanks yes you are correct :)

Was hoping to ask one other question related to this. I am attempting to add a global filter (https://www.polymer-project.org/docs/polymer/expressions.html#writing-global-filters) and use it in my expression as show below. However if I add a filter it produces an error Uncaught TypeError: Cannot read property 'lowercase' of undefined. It does seem to register the filter and I can use it via html but not via JavaScript.  Any suggestion on what I am doing wrong or how to use a custom filter like this?

Thanks again for the help

<!DOCTYPE html>
<html lang="en-US">
<head>
    <title>Expression Test</title>
</head>
<body>
    <script language="JavaScript">
        PolymerExpressions.prototype.lowercase = function(input){
    return input.toLowerCase();
};

        if(PolymerExpressions.getExpression('2 * 3 + X > 2').getValue({ 'X': 44444 })) {
            alert("hit")
        };

        alert(PolymerExpressions.getExpression("ABC | lowercase").getValue({}))
    </script>
</body>
expression.html

Arthur Evans

unread,
Feb 5, 2015, 8:44:46 PM2/5/15
to Ashley Westwell, polymer-dev
Hmm... I'm afraid you've gone beyond my limited knowledge of PolymerExpressions here.

There must be some magic to get the global filters to work outside of a Polymer element, but I don't know what it is.

You can create a local dictionary of filters, and pass that in as the filterRegistry (third argument to getValue). 

Example:

It might work differently if you were importing PolymerExpressions directly, instead of importing Polymer... But I'm speculating here. I've only used and documented Polymer expressions in the context of Polymer data binding.

OK
Arthur


Reply all
Reply to author
Forward
0 new messages