admin管理员组文章数量:1425815
I am trying to migrate to using promises via jQuery. In my original code, I have a callback parameter that takes in modified data:
var getRss = function (url, fnLoad) {
$.get(url, function (data) {
var items = [];
$(data).find('item').each(function (index) {
items.push({
title: $(this).find('title').text(),
pubDate: $(this).find('pubDate').text()
});
});
fnLoad(items);
});
}
I tried changing to promise, but the "done" returns the unmodified data instead of the parsed one:
var getRss = function (url) {
return $.get(url).done(function (data) {
var items = [];
$(data).find('item').each(function (index) {
items.push({
title: $(this).find('title').text(),
pubDate: $(this).find('pubDate').text()
});
});
});
}
Then using it like below but I get the original XML version, not the modified one that was converted to an object:
getRss('/myurl').done(function (data) {
$('body').append(template('#template', data));
});
I am trying to migrate to using promises via jQuery. In my original code, I have a callback parameter that takes in modified data:
var getRss = function (url, fnLoad) {
$.get(url, function (data) {
var items = [];
$(data).find('item').each(function (index) {
items.push({
title: $(this).find('title').text(),
pubDate: $(this).find('pubDate').text()
});
});
fnLoad(items);
});
}
I tried changing to promise, but the "done" returns the unmodified data instead of the parsed one:
var getRss = function (url) {
return $.get(url).done(function (data) {
var items = [];
$(data).find('item').each(function (index) {
items.push({
title: $(this).find('title').text(),
pubDate: $(this).find('pubDate').text()
});
});
});
}
Then using it like below but I get the original XML version, not the modified one that was converted to an object:
getRss('/myurl').done(function (data) {
$('body').append(template('#template', data));
});
Share
Improve this question
edited Feb 25, 2013 at 14:13
TruMan1
asked Feb 25, 2013 at 13:52
TruMan1TruMan1
36.3k64 gold badges204 silver badges364 bronze badges
2
-
you're not accepting the
fnLoad
parameter in your second snippet. – jbabey Commented Feb 25, 2013 at 14:12 - Thx I fixe the typo. In the 2nd snippet, I'm expecting to get back the modified data that the previous "done" performed, but it's always returning the original data throughout the chain. – TruMan1 Commented Feb 25, 2013 at 14:13
1 Answer
Reset to default 7You want to use then
(read the docs for pipe
, see pipe() and then() documentation vs reality in jQuery 1.8):
function getRss(url) {
return $.get(url).then(function (data) {
var items = [];
$(data).find('item').each(function (index) {
items.push({
title: $(this).find('title').text(),
pubDate: $(this).find('pubDate').text()
});
});
return items;
});
}
…which works like
function getRss(url) {
var dfrd = $.Deferred();
$.get(url).done(function (data) {
var items = [];
$(data).find('item').each(function (index) {
items.push({
title: $(this).find('title').text(),
pubDate: $(this).find('pubDate').text()
});
});
dfrd.resolve(items);
}).fail(dfrd.reject);
return dfrd.promise();
}
本文标签: javascriptChange data result while passing promise up chainStack Overflow
版权声明:本文标题:javascript - Change data result while passing promise up chain? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745405756a2657241.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论