1
我目前正在使用JQuery Boilerplate編寫插件。不過,我有問題調用函數不在init()函數內調用。樣板註釋聲明可以通過init函數內的this.functionname()調用函數,但如果從別處調用它,我不確定如何執行此操作。如何在JQuery Boilerplate中使用JQuery Proxy
從這裏查看其他問題,看起來像我可以使用JQuery代理,但是沒有看到如何將其應用於樣板文件的示例。 有人能告訴我,比如我怎麼會內onEnterMobile調用this.createCarousel()或this.settings從請:
;(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);
當您說「從其他地方調用」時,是否可以概述代碼中「其他地方」的實際位置? – iamwhitebox
回答我最後一句中的任何一個問題都是其他地方的例子 –