使用 AJAX 的标准事件消息传递系统?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这值得一看 http://www.jboss.org/errai
I think this is worth looking at http://www.jboss.org/errai
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".
我会投票支持 @dan-heberden 的建议:使用 api.jquery.com/ajaxComplete 或 api.jquery.com/ajaxSucces,例如以下使用 jquery 处理 ajax 事件。
我相信你的问题不需要框架,接受 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.
No frameworks needed for your problem I believe, accept jquery itself.
为什么你想从头开始构建一个消息处理程序,走上一条心痛和痛苦的道路?如果你已经在使用像 jQuery 这样的 js Lib,当然,你有一整套锋利的刀片,比如 全局 Ajax 事件处理程序 和一个 css 库,例如 主题滚轮用于设计您的消息!?
正如其他 SO 伙伴已经提到的:这来自 jQuery 页面:
然后,我能做的,也许是,指出你更好地使用这个工具:
因为你不是在寻找典型的 AJAX 错误,但你想将其用于表单验证目的,我可以建议有这样的东西:
在验证所有数据后,每个
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:
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:
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!