所以我正在学习Riot JS,跟随一个向导。给出了一个逐步解释的例子。并添加一个"this.update()“来更新update变量。现在,这是为他工作,但不是对我。你们能告诉我为什么吗?
这是密码。
这是index.html
<body>
<script src="bower_components/riot/riot.min.js" type="text/javascript"></script>
<script src="tags/all.js" type="text/javascript"></script>
<contact-list></contact-list>
<script>
riot.mount('contact-list', {callback: tagCallback});
function tagCallback(theTag) {
console.log('callback executed');
var request = new XMLHttpRequest();
request.open('GET', 'people.json', true);
request.onload = function() {
if(request.status == 200) {
var data = JSON.parse(request.responseText);
console.log(data);
theTag.trigger('data_loaded', data);
}
}
setTimeout(function() {
request.send();
},2000);
}
</script>
</body>这是我的联系人名单
<contact-list>
<h1>Contacts</h1>
<ul>
<li each={p in opts.people}>{p.first} {p.last}</li>
</ul>
<script>
this.on('mount', function() {
console.log('Riot mount event fired');
opts.callback(this);
})
this.on('data_loaded', function(peeps) {
console.log(peeps);
opts.people = peeps;
this.update();
})
</script>
</contact-list>在使用console.logs进行调试之后,我可以看到我正在正确地从JSON文件中检索数据,我的联系人列表数据就在那里。但子弹名单没有更新。它是空的。
发布于 2016-03-03 13:41:36
哦,伙计们,不好意思。我不知道视频里那个人的例子到底是怎么回事。因为我必须在html触发器事件上传递data.people。否则,我将传递一个平面对象,其中包含一个Array。
发布于 2016-08-24 12:23:26
是否有任何理由使用回调函数?
如果没有,请将回调函数移到标记中,并在将获取的数据分配给标记变量后直接更新它。
查看riotgear中的源代码:https://github.com/RiotGear/rg/blob/master/tags/rg-include/rg-include.tag
对我来说,这是一个完美的例子。
https://stackoverflow.com/questions/35773382
复制相似问题