5 Replies Latest reply on Aug 20, 2015 9:58 AM by brucehul RSS
    hails.alex91@gmail.com Explorer

    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!

      • Re: How to compare two property values in JavaScript code
        paic Collaborator

        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.

        • Re: How to compare two property values in JavaScript code
          brucehul Apprentice

          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.