2 Replies Latest reply on Oct 9, 2017 3:17 AM by carlesc RSS
    hmebane Newbie

    thing template property value

    I want to define a property that applies to all of my Things. So within my base Thing Shape I defined a local property.

     

    Now, I want to define multiple Thing Templates based on that base Thing Shape. I want to set the value of that property differently for each Thing Template.

     

    For example, I have a property called "color" defined in my base "truck" Thing Shape.

     

    I define two Thing Templates called "ups" and "fedex" that implement the "truck" Thing Shape.

     

    I want to set the "color" property all "ups" trucks to "brown", and set the "color" property of all "fedex" trucks to "white".

     

    How do I set those colors in the "ups" and "fedex" thing templates so that all "trucks" that are based on one of those templates gets the property set to the appropriate color?

      • Re: thing template property value
        jamesm Creator

        There is no easy way to override the default value on a property based on a sub template, unfortunately. Keep in mind that templates are meant to be like classes, and don't really have state. That said, here is one way you can accomplish this:

         

        • Create a ThingTemplate called Trucks
          • Add a color property as String
          • Add the following subscription:
            • Source: leave blank
            • Event: ThingStart
            • Enabled: true
            • Script:

                                 if (me.thingTemplate == "UPS") {

                                     me.color = "BROWN";

                                 } else if (me.thingTemplate == "FEDEX") {

                                     me.color = "WHITE";

                                 };

        • Create a ThingTemplate called UPS inheriting from Trucks
        • Create a ThingTemplate called FEDEX inheriting from Trucks
        • Create a UPS thing from UPS Template
        • Create a FedEx Thing from FedEx template
        • The properties should correctly default to BROWN and WHITE respectively.
          • Re: thing template property value
            carlesc Ninja

            Hi Holt,

             

            If you use the solution that James provided, and from this ThingTemplate you subscribe to ThingStart then no one else will be able to subscribe to ThingStart event on this inheritance branch. Knowing this, what we had one it's have a base ThingTemplate ( or a ThingShape --> this is our case ) which subscribes to ThingStart event, then instead of implementing custom code for each child ThingShape / ThingTemplate in the service call itself what we had done its something like:

             

            var implementedShapes = me.GetImplementedShapes();
            for each(implementedShape in implementedShapes.rows) {
              if (me["Init_"+implementedShape.name]!=undefined) {
                 me["Init_"+implementedShape.name]();
                }
            }
            

             

            With the previous code you call for each Implemented Thing Shape the service call "Init_"+ThingShapeName if it exists. On this way you have a more modular code.

             

            You can do the same as before for inherited ThingTemplates ( using baseTemplate property )

             

            This way you have a Generic Initializator for any thing with custom code for each ThingShape / ThingTemplate, this should be on the Standard ThingWorx platform but there isn't ( well or allow to have multiple subscriptions to the same event on the same thing - feature dead on TW 6.5 ).

             

            Best Regards,

            Carles.