Alcadur February 2016

Pass arguments to custom component

I created component and it will be nice to use it ;) but I don't know how to pass arguments;

Use:

<Page xmlns="http://www.nativescript.org/tns.xsd"
      xmlns:masterMenu="includes/customComponents/masterMenu">
    <masterMenu:masterMenu selected="1"/>
</Page>

How can I catch selected in component? Currently I have only masterMenu.xml and masterManu.css files.


[ EDIT ]

Let say I have component named question (pseudo code):

xml

< StackLayout loaded="loaded">

< Label text="Question: loremipsum" />
< Label text="Answer 1" id="answer1" />
< Label text="Answer 2" id="answer2"/>
< Label text="Answer 3" id="answer3"/>

< /StackLayout>

js

exports.loaded = function(args) {

var answerNo = args.answerNo,
    page = args.object;

page.getElementById(page, "answer"+answerNo).addClass('correct') 

}

used :

< Page xmlns="http://www.nativescript.org/tns.xsd" xmlns:question="includes/customComponents/question">

< question:question answerNo="2" />

< question:question answerNo="1" />
< question:question answerNo="3" />

< /Page>

Answers


Brad Martin February 2016

In your javascript you would access it like this:

var menu = page.getViewById("idOfMenuComponent");
menu.selected;

That is assuming it's available to be accessed. That would all depend on how you have this component created. If it's not available you need to add the property to your component. Have a look at the <Button> module to see how this can be done.

Post Status

Asked in February 2016
Viewed 1,929 times
Voted 8
Answered 1 times

Search




Leave an answer