javascript - Change data result while passing promise up chain? - Stack Overflow

I am trying to migrate to using promises via jQuery. In my original code, I have a callback parameter t

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
Add a ment  | 

1 Answer 1

Reset to default 7

You 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();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745405757a4626305.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信