Javascript 如何在JQuery样板中使用JQuery代理
我目前正在使用JQuery样板编写插件。但是,我在调用init()函数中未调用的函数时遇到问题。样板注释说明可以通过init函数中的this.functionname()调用函数,但如果从其他地方调用函数,我不确定如何执行此操作 从这里的其他问题来看,我似乎可以使用JQuery代理来实现这一点,但看不到如何将其应用于样板文件的示例有人能告诉我,例如,我如何从OneTermobile中调用this.createCarousel()或this.settings吗请:Javascript 如何在JQuery样板中使用JQuery代理,javascript,jquery,jquery-plugins,boilerplate,Javascript,Jquery,Jquery Plugins,Boilerplate,我目前正在使用JQuery样板编写插件。但是,我在调用init()函数中未调用的函数时遇到问题。样板注释说明可以通过init函数中的this.functionname()调用函数,但如果从其他地方调用函数,我不确定如何执行此操作 从这里的其他问题来看,我似乎可以使用JQuery代理来实现这一点,但看不到如何将其应用于样板文件的示例有人能告诉我,例如,我如何从OneTermobile中调用this.createCarousel()或this.settings吗请: ;(function ( $,
;(function ( $, window, document, undefined ) {
// Create the defaults once
var pluginName = "dcResponsiveCarousel",
defaults = {
itemsPerPageOnMobile: 1,
itemsPerPageOnTablet: 2,
itemsPerPageOnDesktop: 3
};
// The actual plugin constructor
function Plugin( element, options ) {
this.element = element;
$element = $(element);
this.settings = $.extend( {}, defaults, options) ;
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype = {
init: function() {
enquire
.register("screen and (max-width:767px)", this.onEnterMobile)
.register("screen and (min-width:768px) and (max-width:991px)", this.onEnterTablet)
.register("screen and (min-width:992px)", this.onEnterDesktop)
},
onEnterMobile: function (){
var deviceType = "mobile";
console.log(deviceType);
//this.createCarousel($element, itemsPerPage, deviceType);
}
}
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName,
new Plugin( this, options ));
}
});
};
})( jQuery, window, document );
好的,在这个问题上寻求一些离线帮助之后,我想我应该发布一个示例,说明如何在JQuery样板中使用JQuery代理:
init: function() {
var that = this;
enquire
.register("screen and (max-width:767px)", $.proxy(this.onEnterMobile,this))
.register("screen and (min-width:768px) and (max-width:991px)", $.proxy(this.onEnterTablet,this))
.register("screen and (min-width:992px)", $.proxy(this.onEnterDesktop,this))
},
因此,我不只是调用回调函数this.onEnterMobile,而是从另一个“proxy”函数中调用该函数,该函数允许我传递“this”关键字的值。这允许我使用This.settings从插件中的任何函数访问我的设置,或使用This.functionName调用任何其他函数。functionName当你说“从其他地方调用”时,你能概述一下“其他地方”在代码中的实际位置吗?回答我最后一句中的任何一个问题都是其他地方的例子