I am not that much experienced in riot js. Hierarchy of tags which i created are like
<tag-1>
<tag-2>
<tag-3>
</tag-3>
</tag-2>
</tag-1>
Now i need to pass variable (whcih contains JSON) to "tag-3", and on every update of that variable how can i update "tag-3". Right now i am mounting "tag-3" like
riot.mount('tag-3', { m: "Hello" });
where "m" is variable, and after mount variable "m" is not accessible in tag "tag-3" it show undefined. Another thing, each tag html is in a separate ".tag" and withing that tag i am calling other tag, like in "tag-1.tag" file "tag-2" is called and in "tag-2.tag" file i am calling "tag-3", and in "tag-2.tag" file i am mounting "tag-3"
How can i do that ?
I am not that much experienced in riot js. Hierarchy of tags which i created are like
<tag-1>
<tag-2>
<tag-3>
</tag-3>
</tag-2>
</tag-1>
Now i need to pass variable (whcih contains JSON) to "tag-3", and on every update of that variable how can i update "tag-3". Right now i am mounting "tag-3" like
riot.mount('tag-3', { m: "Hello" });
where "m" is variable, and after mount variable "m" is not accessible in tag "tag-3" it show undefined. Another thing, each tag html is in a separate ".tag" and withing that tag i am calling other tag, like in "tag-1.tag" file "tag-2" is called and in "tag-2.tag" file i am calling "tag-3", and in "tag-2.tag" file i am mounting "tag-3"
How can i do that ?
Share Improve this question edited Aug 28, 2015 at 12:01 Ahmed Khakwani asked Aug 28, 2015 at 11:56 Ahmed KhakwaniAhmed Khakwani 4102 gold badges8 silver badges18 bronze badges3 Answers
Reset to default 5You don't need to mount tags inside other riot tags - only root tags need to be mounted. That's why your
m
parameter doesn't get it intotag-3
.You can have several tag definitions in one .tag file. After tag files are piled, it does not matter whether the riot tags inside were loaded from multiple tag files or not.
You can pass variables as tag attributes - they will be available under
opts
variable.
Putting all together, your single tag file can look like this:
<tag-1>
<p>This is my TAG1</p>
<tag-2 m="{ mValue}"></tag-2>
<script>
this.mValue = { text: 'My text' }
</script>
</tag-1>
<tag-2>
<p>This is my TAG2 with m: { opts.m.text }</p>
<script>
this.on('update', function () {
// you can use 'opts.m' here
});
</script>
</tag-2>
It sounds as thought the value of m will keep changing over time. If that is true, your best bet is the riot.observable() mechanism for sending messages between existing tags.
Whichever tags that are causing the value-change will "trigger" a message of your choice -- perhaps 'value_changed'.
riot.observable().trigger('value_changed', {m: newValueOfComm})
Your tag-3 will "listen" for the message 'value_changed' and do something based on it.
riot.observable().on('value_changed', function(data) { console.log("new value=" + data.m); })
For a working example, check out "Mechanism 2" at: http://vinapps./riot-cookbook-app/#/pages/between-page
The reference page is here: http://riotjs./api/observable/
In Riot version 4 and up, you simply use the attribute to pass anything you want - either a whole JS object, a primitive, or a basic string.
The passed attributes are accessible in the child ponent as this.props.
<parent-ponent>
<inner-ponent data={state.thingToPass} />
</parent-ponent>
If you want the child/inner ponent to respond to changes in their attribute - it is not automatic - you should override the child's onUpdated(props,state)
method and check for changes in the props and react as you wish to the changes.
Of course, you can do it in other ways, too, such as passing observables, and similar tactics, as well as passing an API object, etc. but often, the above way is the simplest for most cases.