我一直在爲一個項目工作一些JavaScript,並決定它應該是一個jQuery插件。我之前寫過一些,但這需要更強大和可銷燬。爲此,我遵循了幾個教程,但是在描述如何銷燬插件時,這些教程都不盡人意。無法銷燬jQuery插件
那麼我該如何銷燬插件?我似乎無法訪問$('.js-target).fullscreen('destroy')
似乎沒有工作。 $(.js-target).data('fullscreen').destroy()
也不在控制檯中返回TypeError: Cannot read property 'destroy' of undefined
。
我已經寫在咖啡腳本。生成的javascript發佈在下面。
(($, window) ->
'use strict'
# Create the defaults once
pluginName = 'fullscreen'
defaults =
reference: window
offset: 0
debug: true
# The actual plugin constructor
Plugin = (element, options) ->
this.element = element
this.options = $.extend {}, defaults, options
this._defaults = defaults
this._name = pluginName
this.init()
Plugin.prototype.init = ->
this.bind()
this.setHeight()
Plugin.prototype.bind = ->
# Maintain the scope
self = this
# Trigger on resize
$(window).on 'resize orientationchange', ->
self.setHeight()
# When scrolling on a touchscreen
# prevent further resizes due to address bar shrinking
$(window).on 'touchstart', ->
self.unbind()
Plugin.prototype.getHeight = ->
this.log 'Get height from: ', this.options.reference
$(this.options.reference).height()
Plugin.prototype.setHeight = ->
if this.options.offset == parseInt(this.options.offset)
offset = this.options.offset
else
offset = 0
$(this.element).css
'min-height' : this.getHeight() - offset
Plugin.prototype.unbind = ->
this.log 'Unbind the resize, touchstart and orientationchange event handlers'
$(window).off 'resize touchstart orientationchange'
Plugin.prototype.destroy = ->
this.unbind()
log 'Remove any heights set on', this.element
$(this.element).attr('style','')
Plugin.prototype.log = (msg, object) ->
if this.options.debug
if !object
object = ''
console.log(pluginName + ': ' + msg, object)
# A really lightweight plugin wrapper around the constructor,
# preventing multiple instantiations
$.fn[pluginName] = (options) ->
return this.each ->
if !$.data(this, 'plugin_' + pluginName)
$.data(this, 'plugin_' + pluginName)
new Plugin(this, options)
return $.fn[pluginName]
) jQuery, window
這是生成的JavaScript。它可能是coffeescript環繞函數的匿名函數嗎?
(function(){
(function($, window) {
'use strict';
var Plugin, defaults, pluginName;
pluginName = 'fullscreen';
defaults = {
reference: window,
offset: 0,
debug: true
};
Plugin = function(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
return this.init();
};
Plugin.prototype.init = function() {
this.bind();
return this.setHeight();
};
Plugin.prototype.bind = function() {
var self;
self = this;
$(window).on('resize orientationchange', function() {
return self.setHeight();
});
return $(window).on('touchstart', function() {
return self.unbind();
});
};
Plugin.prototype.getHeight = function() {
this.log('Get height from: ', this.options.reference);
return $(this.options.reference).height();
};
Plugin.prototype.setHeight = function() {
var offset;
if (this.options.offset === parseInt(this.options.offset)) {
offset = this.options.offset;
} else {
offset = 0;
}
return $(this.element).css({
'min-height': this.getHeight() - offset
});
};
Plugin.prototype.unbind = function() {
this.log('Unbind the resize, touchstart and orientationchange event handlers');
return $(window).off('resize touchstart orientationchange');
};
Plugin.prototype.destroy = function() {
this.unbind();
log('Remove any heights set on', this.element);
return $(this.element).attr('style', '');
};
Plugin.prototype.log = function(msg, object) {
if (this.options.debug) {
if (!object) {
object = '';
}
return console.log(pluginName + ': ' + msg, object);
}
};
$.fn[pluginName] = function(options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName);
return new Plugin(this, options);
}
});
};
return $.fn[pluginName];
})(jQuery, window);
}).call(this);
任何幫助,將不勝感激。
這是一個非常詳細的答案。我很感激你們解釋爲什麼它不起作用,而不是告訴我它應該如何工作。我不熟悉CoffeeScript的細微差別,也不瞭解你提到的巧妙技巧。 – Craig
讓我知道是否有任何技巧(例如'args ...'和'=>'可能)令人困惑,如果您願意,我可以在官方文檔中指出您的位置。我明天下午回來。 –
現在我知道它們存在,我已經在文檔中找到它們。永遠學習!再次感謝。 – Craig