How to expose methods from NativeScript custom components


#1

Basically, the question is asked perfectly here: https://stackoverflow.com/q/38301081/460750

Just that the only answer there doesn’t really answer the question and offers a solution only for decoupled functionality.

I was hoping here would be a good place to find out how to properly expose methods from components.

Thank you!


#2

So you’re wanting to call a method that is within another class? Is that what you’re wanting?

And are you using vanilla nativescript or nativescript with angular?


#3

I was looking for the best (recommended ?) way for my custom components to expose methods to the controllers that need them, other that putting the methods on the root element of the component, as mentioned in the StackOverflow question or making a separate module as in the answer to the question.

As the question mentions, exporting the methods from the module doesn’t add them to the element prototype.

I don’t necessarily mind placing the methods on the root element of my custom component, but I have the same opinion as the question author that it looks kind of hackish and probably there as more “nativescript-way”.

@mast3rd3mon I’m using vanilla JS


#4

Do you mean so you can use the method in the custom component tag in another page?

Ah, vanilla js, not too sure i can help here then. I typically use angular


#5

Can you please take a look at the question on StackOverflow, it gives a perfect example of the thing I want to achieve.


#6

I did. And seen as though the only solution i know is an angular solution, I’m not going to be of any help to you


#7

You might want to answer the question on SO anyway, maybe others will find your Angular solution useful. Also, you made me curious :slight_smile:


#8

Same problem here, is there any solution @surdu?


#9

@clifte Nope. Just exposed some methods from the module, import the module in the controller and use the methods …


#10

But come to think about it, this compromises the reusability nature of the component. I need different components to have their own methods that reflect and affect their own properties.

I’ve found this two new {N} docs pages that seems to be what we need:
https://docs.nativescript.org/plugins/ui-plugin-custom#add-the-ui-bits
https://docs.nativescript.org/core-concepts/properties