问题描述
我使用。我初始化它通过输入code
$(选择)selecter()。
我需要确保formstone selecter jQuery库已完成之前,我开始追加要素。所以,我所做的是要为使用 $。当
函数
初始化:功能(){
。$。当($(选择)selecter()),然后(this.initOptions());
},的initOptions:功能(){
这$ el.find('。selecter')addClass('东西')。
}
但是,这并不正常工作。我怎么能等待formstone selecter是做它的事之前,我执行其他功能?
谢谢,
更新
下面是什么,我做了更新,但它不工作。
初始化:功能(){
$(选择)selecter({回调:this.initOptions});
},的initOptions:功能(){
这$ el.find('。selecter')addClass('东西')。
}
与所提供的代码段的问题是回调的范围:
VAR selectorObj = {
初始化:功能(){
$(选择)selecter({回调:selectorObj.initOptions});
},
的initOptions:功能(){
//这指的是$('。selecter')jQuery的元素
this.addClass('东西');
}
};
不过,如果你只需要添加一个类所呈现的元素,您应该使用'customClass'选项:
$(选择)。selecter({
customClass:东西
});
如果你需要做更多,可以随时直接访问Selecter元素:
变量$ selecter = $(选择)selecter()和Next(selecter)。;$ selecter.addClass(东西)找到(selecter选择。)触发(点击)。
旁注:我Formstone的主要开发者。如果你有新的功能或更好的实施任何建议,只要打开GitHub上一个新的问题。
I'm using selecter 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. So what i did is to is use the $.when
function
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?
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');
}
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:
$("select").selecter({
customClass: "something"
});
If you need to do more, you can always access the Selecter element directly:
var $selecter = $("select").selecter().next(".selecter");
$selecter.addClass("something").find(".selecter-selected").trigger("click");
Sidenote: I'm the main developer of Formstone. If you have any suggestions for new features or better implementation, just open a new issue on GitHub.
这篇关于如何等待一个函数来执行另一个功能之前完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!