使用AJAX完成更新时自动刷新数据块

时间:2012-10-29 18:57:04

标签: php jquery ajax design-patterns

我只有一个页面,其中包含使用AJAX管理的不同部分 在一节(1)中,我添加并编辑公司名称 在以下部分(2)中,我显示了这些公司名称。 (请参阅附件)
当我在第1部分(已编码)中编辑公司名称时,应在第2部分更新新的公司名称。

enter image description here

是否有任何AJAX 模式或策略来实现这一目标?我使用jQuery和PHP ...
THX .-

2 个答案:

答案 0 :(得分:0)

Pattern是这样的,你传递一个使用返回结果的成功函数。:

$.post('your_update_company_url', function(data) {
    $('.company_name').text(data.company_name);
});

答案 1 :(得分:0)

我找到了一个使用我想要分享的发布者/订阅者设计模式的解决方案。

我用一个例子展示它,类似于我在这个问题中提到的:

1.-通过AJAX更新数据:

$.post(window.location.href,
{
    action : 'update_product'
},
function(output) {
    if ( output.result === true ) {
        $.publish("/product/updated",
                {
                    new_name : output.new_name
                });
    }
}, 'json');

2.-刷新页面另一部分的数据:

$.subscribe("/product/updated", function(e, data) {
    $("#product_name").html(data.new_name);
});

这就是全部。这种技术对于用户编码和享受非常有趣......


使用的工具:jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
+信息: Rebecca Murphey 在“Loose Coupling with the pubsub Plugin”中的精彩介绍