1
첫 번째 jquery 플러그인을 작성하고 있습니다. 기본 코딩을 수행했으며 코드를 내부적으로 리팩토링하고 일부 섹션을 함수에 넣기를 원합니다. 플러그인 외부에서 이러한 함수를 호출하고 싶지는 않습니다. 개인 기능, 제 생각에는.jquery 플러그인 내부에서 호출 할 함수를 선언과 혼동했습니다.
(function($) {
$.fn.filterGroup = function(method)
{
var someParam,someOtherParam; //declared here and values updated inside someFunction(), anotherFunction()
return this.each(function()
{
//plugin code starts here
this.someFunction();
alert(someParam); //I want updated value of someParam to be available here
$(inputTextField).keyup(function()
{//as user enters input into an input field
inputText = $(inputTextField).val();
//call a function here on this and does some modification on it.
this.anotherFunction(); //move a lot of lines inside this function
//on subsequent keyups, I want the updated value of someOtherParam to be available here
alert(someOtherParam);
}
});
//not sure where and how to declare these functions ... these needs to be called from inside the plugin only (private functions)
someFunction = function(filterText)
{
//some logic on the passed this, not sure if my sentence is correct in terms of jquery...
//var someParam is updated here
someParam = "something";
}
anotherFunction = function(filterText)
{
//var someOtherParam is updated here
someOtherParam = "something";
}
});
})(jQuery);
내 질문은 - -이 다소 내가 넣을 논리입니다
- 어떻게 내가
this.someFunction();
같이 호출 할 수 있도록 내가는someFunction()
을 정의 할. - 그리고 이후의
onkeyup()
이벤트 동안someParam
의 업데이트 된 값을 읽을 수 있어야합니다.
나는 http://docs.jquery.com/Plugins/Authoring의 의 네임 섹션을 확인하지만 외부에서 호출 할 수있는 공용 함수에 관한 것 같습니다.
또한 이러한 질문 점검 -Using functions from inside a plugin 어떤 다른 사람을하지만 난 혼란 스러워요.
(function($) {
// this object is available only inside of this function
var methods = {
init:function(options){
if(typeof options =="object") settings = $.extend(settings,options);
return this.each(function(){
methods._someFunction();
// all values are available here by private settings object
alert(settings.someParam);
$(this).keyUp(function(){
methods._someOtherFunction();
});
});
},
_someFunction:function(filterText) {
settings.someParam = "something";
},
_anotherFunction:function(filterText) {
settings.someOtherParam = "something";
}
}
// that one too
var settings = {
someParam:null,
someOtherParam:null
}
$.fn.filterGroup = function(method) {
// second condition here will disable calling functions starting from "_".
// ex. `$(selector).filterGroup('_someFunction');` will return error
if(methods[method] && method[0]!=="_") return methods[ method ].apply(this, Array.prototype.slice.call(arguments, 1));
else if (typeof method === 'object' || ! method) return methods.init.apply(this, arguments);
else $.error('Method ' + method + ' does not exist on jQuery.tooltip');
});
})(jQuery);
유의할 것들 : 여기