[英]How to wait for a function to complete before executing another function
I'm using selecter jquery . 我正在使用选择器jquery 。 I initialize it by typing the code
我通过输入代码对其进行初始化
$("select").selecter();
I need to make sure that the formstone selecter jquery library has completed before i start appending elements. 我需要确保在开始添加元素之前,formstone选择器jquery库已完成。 So what i did is to is use the
$.when
function 所以我要做的是使用
$.when
函数
initialize: function(){
$.when($("select").selecter()).then(this.initOptions());
},
initOptions: function(){
this.$el.find('.selecter').addClass('something');
}
But this does not work. 但这是行不通的。 How can i wait while formstone selecter is doing its thing before i execute another function?
在执行其他功能之前,我如何等待formstone selecter做事?
Thanks, 谢谢,
UPDATE 更新
Here's the update of what i did but it does not work. 这是我所做的更新,但不起作用。
initialize: function(){
$("select").selecter({callback: this.initOptions });
},
initOptions: function(){
this.$el.find('.selecter').addClass('something');
}
There is a callback option. 有一个回调选项。
The function passed as a callback will receive the newly selected value as the first parameter
作为回调传递的函数将接收新选择的值作为第一个参数
Should be $("select").selecter(callback: function() { alert('callback fired.') });
应该是
$("select").selecter(callback: function() { alert('callback fired.') });
or as shown 或如图所示
$("select").selecter({
callback: selectCallback
});
function selectCallback(value, index) {
alert("VALUE: " + value + ", INDEX: " + index);
}
The problem which I think regarding the callback edited code is that this can refer to anything. 我认为有关回调编辑代码的问题是,它可以引用任何内容。 Try the following code
试试下面的代码
var selectorObj = {
initialize: function(){
$("select").selecter({callback: selectorObj.initOptions });
},
initOptions: function(){
this.$el.find('.selecter').addClass('something');
}
};
Created a working fiddler for you http://jsfiddle.net/6Bj6j/ 为您创建了一个工作的提琴手http://jsfiddle.net/6Bj6j/
The css is out of shape. CSS变形。 Just select what is poping up when you click on the dropdown.
只需选择单击下拉菜单时弹出的窗口。 You will get an alert which is written in the callback.
您将收到一个写在回调中的警报。
The problem with the provided snippet is the scope of the callback: 提供的代码段的问题在于回调的范围:
var selectorObj = {
initialize: function(){
$("select").selecter({ callback: selectorObj.initOptions });
},
initOptions: function(){
// 'this' refers to the "$('.selecter')" jQuery element
this.addClass('something');
}
};
However if you just need to add a class to the rendered element, you should use the 'customClass' option: 但是,如果您只需要向渲染的元素添加类,则应使用“ customClass”选项:
$("select").selecter({
customClass: "something"
});
If you need to do more, you can always access the Selecter element directly: 如果需要执行更多操作,则始终可以直接访问Selecter元素:
var $selecter = $("select").selecter().next(".selecter");
$selecter.addClass("something").find(".selecter-selected").trigger("click");
Sidenote: I'm the main developer of Formstone. 旁注:我是Formstone的主要开发人员。 If you have any suggestions for new features or better implementation, just open a new issue on GitHub.
如果您对新功能或更好的实现有任何建议,只需在GitHub上打开一个新期刊。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.