Welcome, Guest

Author Topic: Exposing type methods to Javascript  (Read 91 times)

bbcsoft

  • Posts: 1
    • View Profile
Exposing type methods to Javascript
« on: September 26, 2018, 10:40:34 AM »
Hello,

I'm trying to expose a type method to javascript like so:

Code: [Select]
    template <class AbiType, class PropertyType, typename Getter>
    struct AbiGetter : public cohtml::IEventHandler
    {
        AbiGetter(AbiType* obj, Getter g)
        :   m_spObject(obj)
        ,   m_getter(g)
        {}

        virtual void Invoke(cohtml::ArgumentsBinder* binder) override
        {
            assert(false);
        }

        virtual const void* GetTarget() override
        {
            assert(false);
            return nullptr;
        }

        virtual void SetTarget(void* target) override
        {
            assert(false);
        }

        SharedPtr<AbiType> m_spObject;
        Getter m_getter;
    };

void CoherentBind(cohtml::Binder* binder, INode* obj)
{
     if (auto type = binder->RegisterType("INode", obj))
    {
        typedef AbiGetter<AbiType, PropertyType, Getter> GetterClass;
        void* getterPtr = malloc(sizeof(GetterClass));
        GetterClass* pGetter = new (getterPtr) GetterClass(obj, g);
        binder->AddMethod("getEnabled", pGetter);
    }
}


Then in Javascript I do this:

Code: [Select]
    for (var p in ownerNode)
    {
        engine.trigger('JSLog', ownerNode[p]);
    }

It shows me ownerNode only has the type property, namely __Type = INode. But there is no method defined.
Further on, when I try to call it:

Code: [Select]
					
try
{
    ownerNode.getEnabled();
}
catch(err)
{
    engine.trigger('JSLog', 'Exception ' + err.message);
}

I'm getting an Exception ownerNode.getEnabled is not a function.

Any idea what I'm doing wrong?


Stan

  • Administrator
  • *****
  • Posts: 2
    • View Profile
Re: Exposing type methods to Javascript
« Reply #1 on: September 27, 2018, 06:20:03 AM »
Hello bbcsoft,

In order to expose  C++ methods to the JavaScript code, you can simply call `CoherentBind` and bind them with `TypeDescription::Method`.

To better demonstrate this, consider the following code snippet:

Code: [Select]
struct Player
{
 int Value;
 int GetValue()
{
return value;
}
void SetValue(int _value)
{
value=_value;
}
};

void CoherentBind(cohtml::Binder* binder, Player* player)
{
     if (auto type = binder->RegisterType("Player", player))
    {
        type.Property("value" , &Player::Value)
   .Method("getValue", &Player::GetValue)
   .Method("setValue", &Player::SetValue)
   ;
   }
}

After you have bind your methods and properties you should expose the class instance to the JavaScript. This can be achieved via the `View::CreateModel` API.

Code: [Select]
Player m_Player; //Create an instance of the class
m_Player.value = 123;

//Exposes m_Player with the name player to the JavaScript
m_View->CreateModel("player", &m_Player);

Finally, from the JS code, you can access the exposed methods of the model.

Code: [Select]
player.setValue(7); 	//will call m_Player.SetValue(7)
player.getValue(); //will call m_Player.GetValue()
player.value(); //will return m_Player.Value

Keep in mind that you need to include `CoherentHummingbird.js` into the HMTL page in order for the bindings to work.

To get a better idea of how our binding system works, you can also check our documentation where you can find examples of various use cases.

On a side note, if you have any other technical questions, feel free to reach us directly at support@coherent-labs.com and we’ll be happy to assist you!


I hope this helps!


Stan,
Support Engineer at Coherent Labs

Tags: