我需要在加载所有json文件后执行方法callMe()
。
我正在使用Polymer的core-ajax来加载json文件。
我的代码:
<polymer-element name="my-schedule" attributes="speakersUrl sessionsUrl scheduleUrl">
<template>
<core-ajax auto url="{{scheduleUrl}}" handleAs="json" on-core-response="{{scheduleResponse}}"></core-ajax>
<core-ajax auto url="{{sessionsUrl}}" handleAs="json" on-core-response="{{sessionsResponse}}"></core-ajax>
<core-ajax auto url="{{speakersUrl}}" handleAs="json" on-core-response="{{speakersResponse}}"></core-ajax>
</template>
</div>
</template>
<script>
(function() {
Polymer('my-schedule', {
speakersUrl: '../../data/speakers.json',
sessionsUrl: '../../data/sessions.json',
scheduleUrl: '../../data/schedule.json',
speakersResponse: function(event, response) {
this.speakers = response.response;
},
sessionsResponse: function(event, response) {
this.sessions = response.response;
},
scheduleResponse: function(event, response) {
this.schedule = response.response;
},
callMe: function() {
console.log('I was called after all json files loaded');
}
});
})();
</script>
谁可以帮助我? 注意:没有jQuery。
答案 0 :(得分:2)
这应该可以解决问题。通过使用observe
,您可以在每次更改时检查值并等待所有三个值出现。您也可以直接绑定到response
的{{1}}属性,而不必使用设置器。
core-ajax
答案 1 :(得分:0)
有两种方法:
检查数据何时准备就绪 - &gt;调用custom_element,其中在ready()回调中调用callMe()。
<my-template if="{{data_json.list}}">
<my-custom_element
pass_data ='{{data_json.list}}'
></my-custom_element></template>
类似:Polymer, how to wait for core-ajax to finish before rendering other elements?
答案 2 :(得分:0)
我认为我要做的是制作3个真/假变量,并在每次加载后将加载项的状态设置为true并检查其他项的状态。一旦你可以使用另外一个变量来检查方法是否已经运行,以防止它再次发射。
示例
<polymer-element name="my-schedule" attributes="speakersUrl sessionsUrl scheduleUrl">
<template>
<core-ajax auto url="{{scheduleUrl}}" handleAs="json" on-core-response="{{scheduleResponse}}"></core-ajax>
<core-ajax auto url="{{sessionsUrl}}" handleAs="json" on-core-response="{{sessionsResponse}}"></core-ajax>
<core-ajax auto url="{{speakersUrl}}" handleAs="json" on-core-response="{{speakersResponse}}"></core-ajax>
</template>
<script>
(function() {
Polymer('my-schedule', {
speakersUrl: '../../data/speakers.json',
speakerStatus: false,
sessionsUrl: '../../data/sessions.json',
sessionStatus: false,
scheduleUrl: '../../data/schedule.json',
scheduleStatus: false,
hasRun: false,
speakersResponse: function(event, response) {
this.speakers = response.response;
this.speakerStatus = true;
this.checkStatus();
},
sessionsResponse: function(event, response) {
this.sessions = response.response;
this.sessionStatus = true;
this.checkStatus();
},
scheduleResponse: function(event, response) {
this.schedule = response.response;
this.scheduleStatus = true;
this.checkStatus();
},
checkStatus: function () {
if (this.speakerStatus && this.sessionStatus && this.scheduleStatus && !this.hasRun) {
this.callMe();
this.hasRun = true;
}
},
callMe: function() {
console.log('I was called after all json files loaded');
}
});
})();
</script>
</polymer-element>
答案 3 :(得分:0)
您可以在core-ajax元素上使用 on-core-complete 事件来检查json何时加载。
E.G <core-ajax auto url="{{scheduleUrl}}" handleAs="json" on-core-complete="{{scheduleResponse}}"></core-ajax>
了解详情:https://www.polymer-project.org/0.5/docs/elements/core-ajax.html