플러그인에서 메소드 내부의 하위 메소드에 액세스하려면 어떻게해야합니까? 예를 들어, init
방법, 나는jquery 플러그인에서 메소드 내부의 하위 메소드에 액세스하는 방법은 무엇입니까?
$this.cms(manage.page); // ReferenceError: manage is not defined
기본 구조,
(function($){
var methods = {
defaults: function(options) {
// Default options.
var defaults = {
setup: {
directory: ""
}
}
// Always leave this line here.
var options = $.extend(defaults, options);
// Return the processed options.
return options;
},
init : function(options) {
// Must declare a this as the plugin's root itself.
var $this = this;
// Call the local method from the plugin itself to get the processed options.
var o = $this.cms('defaults',options);
//alert("nothing to load");
$this.cms(manage.page);
},
manage : {
page: function(options){
// Must declare a this as the plugin's root itself.
var $this = this;
// Call the local method from the plugin itself to get the processed options.
var o = $this.cms('defaults',options);
alert("page method");
},
menu: function(options){
}
},
add : function(options) {
},
erase : function(options) {
},
remove: function(object,options) { // to be developed.
}
};
$.fn.cms = function(method) {
// @reference: http://docs.jquery.com/Plugins/Authoring#Namespacing
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments); // always change 'init' to something else if you different method name.
} else {
$.error('Method ' + method + ' does not exist on jQuery.cms');
}
return this; // this is needed to ensure chainability @reference: http://stackoverflow.com/questions/14008121/jquery-namespace-how-to-pass-default-options-from-one-method-to-the-subsequence
};
})(jQuery);
이 가능하거나 일을 다른 더 좋은 방법인가, manage
내부의 page
방법에 액세스하려면? 자식 메서드를 부모 메서드 안에 넣고 싶습니다.
감사합니다. 'manage' 객체에'page()'를 어떻게 두어야합니까? 몇 가지 예를 보여 주시겠습니까 ...? – laukok
그는 사용할 수 없습니다 :'methods.manage.page.apply (this, [o]); ' –
@lauthiamkok 기본 JavaScript 질문입니다. 자바 스크립트에서 객체가 작동하는 방식을 연구하는 것이 좋습니다. 'manage' 속성을 새로운 인터페이스를 반환하는 함수로 만들 수 있습니다. 그런 다음 메서드를 호출합니다 (그렇게하면 * jQuery 체인 외부에있을 것입니다). – alex