使用 AJAX 的标准事件消息传递系统?

发布于 2024-09-03 12:40:39 字数 2396 浏览 3 评论 0原文

AJAX 是否有任何标准或消息传递框架?

现在我有一个使用 Ajax 加载内容的页面。因为我的内容中有一个复杂的数据输入表单,所以我需要验证表单中可能发生的某些事件。 ,经过我的测试驱动的一些调整后:

asyncShould("search customer list click", 3, function() {
    stop(1000);
    $('#content').show();
    var forCustomerList = newCustomerListRequest();
    var forShipAndCharge = newShipAndChargeRequest(forCustomerList);

    forCustomerList.page = '../../vt/' + forCustomerList.page;
    forShipAndCharge.page = 'helpers/helper.php';
    forShipAndCharge.data = { 'action': 'shipAndCharge', 'DB': '11001' };

    var originalComplete = forShipAndCharge.complete;

    forShipAndCharge.complete = function(xhr, status) {
        originalComplete(xhr, status);
        ok($('#customer_edit').is(":visible"), 'Shows customer editor');
        $('#search').click();
        ok($('#customer_list').is(":visible"), 'Shows customer list');
        ok($('#customer_edit').is(":hidden"), 'Does not show customer editor');
        start();
    };

    testController.getContent(forShipAndCharge);
});

这是用于获取内容的控制器:

    getContent: function (request) {
        $.ajax({
            type:       'GET',
            url:        request.page,
            dataType:   'json',
            data:       request.data,
            async:      request.async,
            success:    request.success,
            complete:   request.complete
        });
    },

这是请求事件:

function newShipAndChargeRequest(serverRequest) {
var that = {
    serverRequest: serverRequest,
    page: 'nodes/orders/sc.php',
    data: 'customer_id=-1',
    complete: errorHandler,
    success: function(msg) {
        shipAndChargeHandler(msg);
        initWhenCustomer(that.serverRequest);
    },
    async: true
};
return that;

}

这是一个成功处理程序:

function shipAndChargeHandler(msg) {
    $('.contentContainer').html(msg.html);
    if (msg.status == 'flash') {
        flash(msg.flash);
    }
}

在我的服务器端,我最终得到一个如下所示的 JSON 结构:

$message['status'] = 'success';
$message['data'] = array();
$message['flash'] = '';
$message['html'] = '';
echo json_encode($message);

因此 现在加载的内容由两部分组成:

  • HTML,这是表单的呈现。
  • DATA,这是需要为表单
  • FLASH 加载的任何数据,任何验证或服务器错误
  • STATUS 告诉客户端服务器上发生了什么。

我的问题是:这是在客户端处理事件消息传递的有效方法还是我会走上一条心痛和痛苦的道路?

Is there any standards or messaging framework for AJAX?

Right now I have a single page that loads content using Ajax. Because I had a complex form for data entry as part of my content, I need to validate certain events that can occur in my form. So after some adjustments driven by my tests:

asyncShould("search customer list click", 3, function() {
    stop(1000);
    $('#content').show();
    var forCustomerList = newCustomerListRequest();
    var forShipAndCharge = newShipAndChargeRequest(forCustomerList);

    forCustomerList.page = '../../vt/' + forCustomerList.page;
    forShipAndCharge.page = 'helpers/helper.php';
    forShipAndCharge.data = { 'action': 'shipAndCharge', 'DB': '11001' };

    var originalComplete = forShipAndCharge.complete;

    forShipAndCharge.complete = function(xhr, status) {
        originalComplete(xhr, status);
        ok($('#customer_edit').is(":visible"), 'Shows customer editor');
        $('#search').click();
        ok($('#customer_list').is(":visible"), 'Shows customer list');
        ok($('#customer_edit').is(":hidden"), 'Does not show customer editor');
        start();
    };

    testController.getContent(forShipAndCharge);
});

Here is the controller for getting content:

    getContent: function (request) {
        $.ajax({
            type:       'GET',
            url:        request.page,
            dataType:   'json',
            data:       request.data,
            async:      request.async,
            success:    request.success,
            complete:   request.complete
        });
    },

And here is the request event:

function newShipAndChargeRequest(serverRequest) {
var that = {
    serverRequest: serverRequest,
    page: 'nodes/orders/sc.php',
    data: 'customer_id=-1',
    complete: errorHandler,
    success: function(msg) {
        shipAndChargeHandler(msg);
        initWhenCustomer(that.serverRequest);
    },
    async: true
};
return that;

}

And here is a success handler:

function shipAndChargeHandler(msg) {
    $('.contentContainer').html(msg.html);
    if (msg.status == 'flash') {
        flash(msg.flash);
    }
}

And on my server side I end up with a JSON structure that looks like this:

$message['status'] = 'success';
$message['data'] = array();
$message['flash'] = '';
$message['html'] = '';
echo json_encode($message);

So now loading content consists of two parts:

  • HTML, this is the presentation of the form.
  • DATA, this is any data that needs be loaded for the form
  • FLASH, any validation or server errors
  • STATUS tells client what happened on server.

My question is: Is this a valid way to handle event messaging on the client-side or am I going down a path of heartache and pain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

关于从前 2024-09-10 12:40:39

我认为这值得一看 http://www.jboss.org/errai

I think this is worth looking at http://www.jboss.org/errai

情域 2024-09-10 12:40:39

OpenAjax Hub(来自 OpenAjax 联盟)指定基于发布/订阅的事件中心(主题总线)。查看开源参考实现:http://openajaxallianc.sourceforge.net/

TIBCO 还有一个用 JavaScript 实现的事件总线,称为 PageBus 但我想上面的内容更多“标准”。

The OpenAjax Hub (from the OpenAjax Alliance) specifies a publish/subscribe-based event hub (topic bus). Have a look at the open-source reference implementation: http://openajaxallianc.sourceforge.net/.

TIBCO also has an event bus implemented in JavaScript called PageBus but I guess the above is more "standard".

裂开嘴轻声笑有多痛 2024-09-10 12:40:39

我会投票支持 @dan-heberden 的建议:使用 api.jquery.com/ajaxComplete 或 api.jquery.com/ajaxSucces,例如以下使用 jquery 处理 ajax 事件。

$('.contentContainer').ajaxSuccess(function(e, xhr, settings) {
  var msg.html = "<p>You did it!</p>";
  shipAndChargeHandler(msg);
});

我相信你的问题不需要框架,接受 jquery 本身。

I would vote for the suggestion by @dan-heberden: using api.jquery.com/ajaxComplete or api.jquery.com/ajaxSucces, like for example the following to handle ajax events with jquery.

$('.contentContainer').ajaxSuccess(function(e, xhr, settings) {
  var msg.html = "<p>You did it!</p>";
  shipAndChargeHandler(msg);
});

No frameworks needed for your problem I believe, accept jquery itself.

明明#如月 2024-09-10 12:40:39

为什么你想从头开始构建一个消息处理程序,走上一条心痛和痛苦的道路?如果你已经在使用像 jQuery 这样的 js Lib,当然,你有一整套锋利的刀片,比如 全局 Ajax 事件处理程序 和一个 css 库,例如 主题滚轮用于设计您的消息!?

正如其他 SO 伙伴已经提到的:这来自 jQuery 页面:

当 Ajax 请求时显示消息
成功完成。

$("#msg").ajaxSuccess(function(evt, request, settings){
      $(this).append("<li>Successful Request!</li>");
      });

然后,我能做的,也许是,指出你更好地使用这个工具:

因为你不是在寻找典型的 AJAX 错误,但你想将其用于表单验证目的,我可以建议有这样的东西:

$.ajaxComplete(function(event, xhr, options) {
    var data = xhr.responceText;
    switch(data) {
    case 'err_1': 
    // do_something(1)
    break;
    case 'err_2': 
    // do_something(2)
    break;
    case 'err_3': 
    // do_something(3)
    break; 
    }
});

在验证所有数据后,每个 err_# 来自后端,如果发现错误,您通过 ajax 发送代码错误,其余的都是糖! ;)

PS:通过这样做,即使 javascript 被禁用,您也可以防止滥用!

why you want go down a path of heartache and pain by builting one msg handler from scratch? if you already are using a js Lib like jQuery, and of course, you have a complete set of sharpened blades like Global Ajax Event Handlers and a css lib like Theme Roller for style your messages!?

As already mentioned by other SO mates: this come from the jQuery page:

Show a message when an Ajax request
completes successfully.

$("#msg").ajaxSuccess(function(evt, request, settings){
      $(this).append("<li>Successful Request!</li>");
      });

Then, what i can do, is maybe, pointing you to use better this tools es.:

since you are not looking for a typical AJAX error, but you want use this for form validation purpose, i can suggest to have something like this:

$.ajaxComplete(function(event, xhr, options) {
    var data = xhr.responceText;
    switch(data) {
    case 'err_1': 
    // do_something(1)
    break;
    case 'err_2': 
    // do_something(2)
    break;
    case 'err_3': 
    // do_something(3)
    break; 
    }
});

Where each err_# come from backend after you have validated all data, if error found, you send via ajax the code error the rest is sugar! ;)

PS: by doing this you can prevent abuse also if javascript is disabled!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文