要获得标签实例的引用,您必须这样做。 tags 将是一个带有标签的数组。
riot.compile(function() {
tags = riot.mount('*')
console.log('root tag',tags[0])
})
如果你想访问孩子,假设 vader 是父标签,leia 和 luke 孩子标签
riot.compile(function() {
tags = riot.mount('*')
console.log('parent',tags[0])
console.log('children',tags[0].tags)
console.log('first child by name',tags[0].tags.luke)
console.log('second child by hash',tags[0].tags['leia'])
})
但我会推荐标签通信的可观察模式。很简单
1) 创建 store.js 文件
var Store = function(){
riot.observable(this)
}
2)在索引中将它添加到全局 riot 对象中,这样它就可以在任何地方访问
<script type="text/javascript">
riot.store = new Store()
riot.mount('*')
</script>
3)然后在任何标签中你都可以:
riot.store.on('hello', function(greeting) {
self.hi = greeting
self.update()
})
4)并且在其他标签中有:
riot.store.trigger('hello', 'Hello, from Leia')
所以你使用 riot.store 全局对象进行通信,发送和接收消息
现场示例http://plnkr.co/edit/QWXx3UJWYgG6cRo5OCVY?p=preview
在你的情况下,使用 riot.store 是一样的,可能你需要使用 self 来不丢失上下文引用
<script>
var self = this
this.warning_message = "Default warning!";
riot.store.on('updateMessage', function(message){
self.warning_message = message;
});
</script>
然后从任何其他标记调用
riot.store.trigger('updateMessage', 'Hello')