你能够经过this.refs对象访问dom元素jquery
并且还有大量的属性简写方式能够使用浏览器
好比:if="{...}",(有时候你须要对这些东西作一些特殊的处理才能用)app
若是你想在riot标签内部访问dom元素dom
你可能须要了解一下riot标签生命周期相关的知识函数
你会注意到,mount方法还没执行的时候,dom元素是不会被建立的this
这就意味着,mount方法以前访问DOM元素,是不会成功的code
请看以下代码:htm
<example-tag> <p id="findMe">Do I even Exist?</p> <script> var test1 = document.getElementById('findMe') console.log('test1', test1) // Fails this.on('update', function(){ var test2 = document.getElementById('findMe') console.log('test2', test2) // Succeeds, fires on every update }) this.on('mount', function(){ var test3 = document.getElementById('findMe') console.log('test3', test3) // Succeeds, fires once (per mount) }) </script> </example-tag>
也就是说,你只要在正确的函数中使用jquery是一点问题都没有的;对象
再看下面的代码(两种检索方式都是支持的,第一种就是jquery检索DOM)
<example-tag> <p id="findMe">Do I even Exist?</p> <p>Is this real life?</p> <p>Or just fantasy?</p> <script> this.on('mount', function(){ // Contexted jQuery $('p', this.root) // Contexted Query Selector this.root.querySelectorAll('p') }) </script> </example-tag>
你能够在初始化的时候为riotjs标签传入更多参数,好比:
<script> riot.mount('todo', { title: 'My TODO app', items: [ ... ] }) </script>
你能够传递任何类型的数据;
能够是一个简单的object;
也能够是动态变化的数据存储(flux store)
在标签内部,你能够使用以下方法访问这些输入参数
<my-tag> <!-- Options in HTML --> <h3>{ opts.title }</h3> // Options in JavaScript var title = opts.title </my-tag>
riotjs标签按照以下步骤构造及渲染
一个riotjs标签在浏览器上渲染,mount事件触发后,什么时候会被更新呢?
当一个组件执行更新后,会触发update事件
<todo> this.on('before-mount', function() { // before the tag is mounted }) this.on('mount', function() { // right after the tag is mounted on the page }) this.on('update', function() { // allows recalculation of context data before the update }) this.on('updated', function() { // right after the tag template is updated after an update call }) this.on('before-unmount', function() { // before the tag is removed }) this.on('unmount', function() { // when the tag is removed from the page }) // curious about all events ? this.on('*', function(eventName) { console.info(eventName) }) </todo>
你能够为一个事件设置多个监听