admin管理员组文章数量:1405908
I have a single page application using vue js. Now I have to get data from 3 different source URL and then need to map in an object to use it on the application.
Or is it better to get it from ONE URL after mapping it on the backend?
$.get(furl, function(data) {
this.items1 = data;
});
$.get(furl, function(data) {
this.items2 = data;
});
$.get(furl, function(data) {
this.items3 = data;
});
// if I want to merge it here. I am not getting items1, items2, items3 here.
<script src=".1.1/jquery.min.js"></script>
I have a single page application using vue js. Now I have to get data from 3 different source URL and then need to map in an object to use it on the application.
Or is it better to get it from ONE URL after mapping it on the backend?
$.get(furl, function(data) {
this.items1 = data;
});
$.get(furl, function(data) {
this.items2 = data;
});
$.get(furl, function(data) {
this.items3 = data;
});
// if I want to merge it here. I am not getting items1, items2, items3 here.
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Share
Improve this question
edited Sep 28, 2017 at 18:47
TheOdd
2652 silver badges17 bronze badges
asked Sep 28, 2017 at 18:37
ParkarParkar
3,0463 gold badges17 silver badges24 bronze badges
3 Answers
Reset to default 6Use Promises:
Promise.all([
new Promise(function(resolve) {
$.get( furl, function( data ) {
resolve(data);
});
}),
new Promise(function(resolve) {
$.get( furl, function( data ) {
resolve(data);
});
}),
new Promise(function(resolve) {
$.get( furl, function( data ) {
resolve(data);
});
})
]).then(function(results) {
// The items will be available here as results[0], results[1], results[2], etc.
});
Written more efficiently and elegantly:
function promisifiedGet(url) {
return new Promise(function(resolve) {
$.get(url, resolve);
});
}
Promise.all([
promisifiedGet(furl1),
promisifiedGet(furl2),
promisifiedGet(furl3)
]).then(function(results) {
console.log(results);
});
It looks like you are using jQuery for the ajax calls? In this case, I would remend using the when() to which will monitor the deffered objects that e back from the ajax queries and wait for them all to plete.
var a1 = $.get( furl, function( data ) {
this.items1 = data;
});
var a2 = $.get( furl, function( data ) {
this.items2 = data;
});
var a3 = $.get( furl, function( data ) {
this.items3 = data;
});
$.when(a1, a2, a3, function(data1, data2, data3) {
// here is where you merge them into your object and continue the rest of your code
var o = {};
o.items1 = data1;
o.items2 = data2;
o.items3 - data3;
});
You are using this
inside a function, which in tern might be Either the global object or undefined if there is a use strict
directive. Have you tried creating a variable out of the callback functions and in each callback, add the data
to that array?
Like so:
let results = {};
$.get( furl, function( data ) {
results.items1 = data;
});
$.get( furl, function( data ) {
results.items2 = data;
});
$.get( furl, function( data ) {
results.items3 = data;
});
本文标签: jqueryHow to map URL data in a Single object using JavascriptStack Overflow
版权声明:本文标题:jquery - How to map URL data in a Single object using Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744937261a2633264.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论