自定义JS确认使用jQuery.Deferred和问题与返回值情态动词的基础上按钮 [英] Custom JS Confirm Modals using jQuery.Deferred and issues with return values based on buttons
问题描述
我是谁最近有点进入了世界的Javascript Java开发人员,所以请原谅我,如果我做了一些愚蠢的错误或假设。我试图把我们的自带浏览器/ js的确认对话框已使用主要骨干框架开发的自定义模态。目前,这些模态不支持确定类型的功能。我认为首要的问题是,我的情态动词不是目前的工作是异步的。
I'm a java developer who has somewhat recently entered the Javascript world, so forgive me if I make some silly mistakes or assumptions. I am attempting to translate our native browser/js "Confirm" dialog boxes to custom modals that have been developed using primarily the backbone framework. Currently, these modals do not support "confirm" type functionality. I think the primary issue is that my modals are not currently working asynchronously.
我有我打电话,打开一个模态的方法,而我希望从依赖于用户点击了哪个按钮呼叫收到一个真或假值回。但是,目前我的方法尽快将显示模式返回,而不会等待点击一个按钮。模态系统支持每个按钮,它可以调用openPortletModal方法时被定义,正如我在下面展示独立回调。然而,confirmDiscard方法返回未定义的值,而没有等待任何按钮调用。我的问题是,是否有可能暂停的方法执行,直到回调是由?
I have a method that I am calling that opens a modal, and I am hoping to receive a "true" or "false" value back from the call which depends on which button the user clicked. However, currently my method returns as soon as the modal is displayed, and does not wait for a button click. The modal system supports seperate callbacks for each button, which can be defined when calling the "openPortletModal" method, as I will demonstrate below. However, the "confirmDiscard" method returns an undefined value, without ever waiting for any of the button calls. My question is, is it possible to pause execution of a method until a callback is made?
虽然研究,我也打听了一下关于jQuery.Deferred,但我还没有发现利用它来得到我想要的功能,清晰的方式,而没有例子我看似乎基于回调,包括不同的返回值
While researching, I did find out a bit about jQuery.Deferred,but I haven't found a clear way of utilizing it to get the functionality I want, and no examples I see seem to include different return values based on callbacks.
下面是我的示例code:
Here's my sample code:
confirmDiscard: function(context) {
var deferred = new $.Deferred();
MyModal.openPortletModal(context, null, {
views: [{
name: "auth",
title: "Confirmation",
renderResponseData: "This is a test", //Message that modal renders
buttons: [{
type: "FINISH",
label: "OK",
click: function() {
console.log("CLICKED OK");
deferred.resolve("true"); //this is ultimately what I'd like to return from the confirmDialog() method
return false; //Modal system expects a "false" value from callback if there is no AJAX call to be made.
},
}, {
type: "CANCEL",
click: function() {
console.log("CLICKED CANCEL");
deferred.resolve("false");
return false;
},
}]
}]
});
deferred.done(function(value) {
alert(value); //This does get called on button click, but by now the confirmDiscard method has already returned.
return value;
})
return deferred.promise();
},
由于这种模式,有没有在JavaScript的方式同时进行模态响应有这个方法的等待?
Given this pattern, is there a way in javascript to have this method wait while the modal is responded to?
推荐答案
我劝你喜欢你的一种方式,但有一点不同。我希望它帮助你。
i advise you a way like your, but a little different. I hope it help you
cutomConfirm: function(text) {
var d, rendered, template,
_this = this;
template = $('#custom_confirm_temp');
rendered = $.tmpl($(template).template(), {
text: text || ''
});
$.blockUI({
message: rendered,
showOverlay: true,
onUnblock: function() {
return $(document).unbind('click');
},
css: {
width: '600px',
height: '150px',
opacity: '1',
border: '0',
backgroundColor: 'none'
}
});
d = $.Deferred();
rendered.find('#OK').click(function() {
$.unblockUI();
return d.resolve();
});
rendered.find('#CANCEL').click(function() {
$.unblockUI();
return d.reject();
});
return d;
}
您需要创建模板(使用id =custom_confirm_temp),当你需要调用!
You need to create your template(with id ="custom_confirm_temp" ) and call when you need !
这篇关于自定义JS确认使用jQuery.Deferred和问题与返回值情态动词的基础上按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!