【发布时间】:2014-09-18 14:24:35
【问题描述】:
我一直在为一个项目开发一些 javascript,并决定它应该是一个 jQuery 插件。我以前写过一些,但这需要更加健壮和可破坏。为此,我遵循了一些教程,但在描述如何销毁插件时,它们都不够完善。
那么我该如何销毁插件呢?我似乎无法访问$('.js-target).fullscreen('destroy') 似乎不起作用。 $(.js-target).data('fullscreen').destroy() 也不会在控制台中返回 TypeError: Cannot read property 'destroy' of undefined。
我用coffeescript 写的。生成的 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);
任何帮助将不胜感激。
【问题讨论】:
标签: jquery jquery-plugins coffeescript destroy