Creating and linking function in Ignite

Not applicable

How do I create a simple function e.g "1 + 2 = 3" and link it to a button using apigee ignite?

Thanks.

2 2 251
2 REPLIES 2

@Jordan Lin

You can do something like this:

{
            "_id": "buttonOne",
            "_type": "Button",
            "attributes": {
              "layoutType": "absolute",
              "align.v": "top",
              "margin.left": 200,
              "size.w": 40,
              "size.h": 20,
              "text.align": "center",
              "font": "HelveticaNeue-Bold:18",
              "text": "My Button",
              "margin.top": "0",
              "bg.color": "#fff",
              "color": "#898989",
              "border.radius": 5,
              "border.color": "#000",
              "border.size": "2",
              "padding.bottom": 4
            },
            "actions": [
              {
                "_type": "Modify",
                "attributes": {
                  "_target": "resultTextField"
                },
                "set": {
                  "text": "{{Number([[inputTextField.text]])+1}}"
                },
                "on": "touchUp"
              },
              {
                "_type": "Refresh",
                "attributes": {
                  "_target": "otherTextField"
                },
                "on": "touchUp"
              }
            ]
}

In this example when the button is pressed, the value from inputTextField is read and one is added and the result is written to resultTextField. When you just want to recalculate the value of a field then you can use the Refresh function.

Not applicable

@Jordan Lin @kbouwmeester

If you need have more complex JavaScript and want to use JavaScript functions, you can use a $session variable to store your functions and pull them in where needed.

For example, store your function(s) in a $session variable..

{
  "_type": "Modify",
  "attributes": {
    "_target": "$session"
  },
  "set": {
    "js": "function toCelsius(fahrenheit) {return (5/9) * (fahrenheit-32);}"
  },
  "on": "willAppear"
}

..and then later use the function by including the session variable in another JavaScript evaluation:

{
  "_type": "Alert",
  "attributes": {
    "title": "{{ [[$session.js]]; toCelsius(32);}}"
  },
  "enabled": true,
  "on": "touchUp"
}