cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

How to compare two property values in JavaScript code

hails.alex91@gm
1-Newbie

How to compare two property values in JavaScript code

Hello everyone,

I am trying to compare two values that I have created in my widget (code below): User & Sender

      "properties": {

        "Timestamp":{

          "baseType":"DATETIME",

          "defaultValue": "08/18/2015"

        },

        "Sender":{

          "baseType": "STRING",

          "defaultValue": "User1"

        },

        "Message": {

          "baseType": "STRING",

          "defaultValue": "blah blah blah"

        },

        "User":{

           "baseType": "STRING",

           "defaultValue":"User1"

        }

I am trying to compare them using this expression, which is within script tags that I return when I render:  "if("+ this.getProperty("User")+"=="+this.getProperty("Sender")+"){"

But i get an error upon load stating that User1 cannot be referenced....any thoughts on how I can compare the values of two properties?

Thanks!

5 REPLIES 5

Is there a reason you need to do this client side?

Generally I would recommend the use of the Validation widget if you have to do this client side.

Most often though we recommend to do these things server side.

I guess please describe what your widget does/use case.

Hi Pai,

Still a bit new to TW so bear with me. I am not sure what you mean by doing things on the server-side. I understand what server-side means, but i do not know how to access it/interact with it.

The widget is simple...I have two stylized message boxes: 1 for displaying messages sent, 1 for displaying messages received. The message box that the data flows into will be driven by who the current user is, and who sent the message. If the sender & the user are the same, then display the message in the "sent" stylized box. Else, display the message in the "received" stylized box.

I hope this helps!

I would just use two message boxes and some Services for that. In an extreme case I would utilize a Validation widget.

What do you mean when you say use some services? Write custom services?

Given that you have the objects User and Sender in properties and each of those has defaultValue, I'd recommend this as the comparison:

if(properties.User.defaultValue===properties.Sender.defaultValue){ ...something...}

If you're not trying to compare the defaultValue properties, then you must have done something like properties.User.value=something; elsewhere in your code and you would be comparing value instead of defaultValue.

Also remember that the way javascript objects work, they could be arrays or maps (your declaration is for a map because a squiggly brace follows properties: instead of a square bracket), so you could also do:

var item1='User';

var item2='Sender';

if(properties[item1].defaultValue===properties[item2].defaultValue){ ...something...}

If you're not that familiar with javascript objects, you might want to check any of the numerous javascript tutorials and training classes available on the web.

As for the client/server side issue elsewhere in this thread, extensions to the UI will be running on the client (on the HTML page in the browser). All other extensions run in the server. When you create a Thingworx Thing and create a service using javascript, this is really running inside the servlet engine where you deployed Thingworx.war (server side). That's one big reason why you can't control widgets from your services - they only create data objects like INFOTABLEs that are sent back to the client.

Top Tags