json - Combining Two JQuery.Map into one? -
im doing calls spotify webservice search artists , tracks in personal application.
the problem want display 2 different results in 1 object.
is possible?
or should approach in different way? (get me on track in case).
maybe should nested call webservice ?(please write code, i've tried myself, dont know how right)
jsfiddle readability , demo: http://jsfiddle.net/a97ys/
code below
function gettracks(request, response) { $.get("http://ws.spotify.com/search/1/track.json", { q: request.term }, function (data) { response($.map(data.tracks.slice(0, 5), function (item) { return { label: item.name, by: item.artists[0].name, category: "track" }; })); }); } function getartist(request, response) { $.get("http://ws.spotify.com/search/1/artist.json", { q: request.term }, function (data) { response($.map(data.artists.slice(0, 5), function (item) { return { label: item.name, by: "", category: "artist" }; })); }); }
this function 1 have problem with:
$(function () { $("#listentoinput").catcomplete({ delay: 0, source: function (request, response) { //...combine 2 maps , return 1 object ...// } }); });
when you're using autocomplete, it's nice keep local cache variable store results in. can use same cache combine track , artist search results so:
var cache = {}; function gettracks(request, response) { $.get("http://ws.spotify.com/search/1/track.json", { q: request.term }, function (data) { $.merge(cache[request.term], $.map(data.tracks.slice(0, 5), function (item) { return { label: item.name, by: item.artists[0].name, category: "track" }; })); response(cache[request.term]); }); } function getartist(request, response) { $.get("http://ws.spotify.com/search/1/artist.json", { q: request.term }, function (data) { $.merge(cache[request.term], $.map(data.artists.slice(0, 5), function (item) { return { label: item.name, by: "", category: "artist" }; })); response(cache[request.term]); }); } $(function () { $("#listentoinput").catcomplete({ delay: 0, source: function (request, response) { if (cache[request.term]) { return response(cache[request.term]); } else { cache[request.term] = []; gettracks(request, response); getartist(request, response); } } }); });
autocomplete doesn't seem complain if response function gets called twice.
Comments
Post a Comment