Skip to content

Instantly share code, notes, and snippets.

@benvds
Last active August 29, 2015 13:57
Show Gist options
  • Save benvds/9760804 to your computer and use it in GitHub Desktop.
Save benvds/9760804 to your computer and use it in GitHub Desktop.
/* global jQuery */
;(function(window, $) {
'use strict';
var firstPrivate = 1,
secondPrivate = '2',
defaults = {
'firstOption': false,
'secondOption': 5000
},
// constructor
MyComponent = function (element, options) {
// source element for the slideshow
this.$element = $(element);
// supplement options with defaults
this.options = $.extend({}, defaults, options);
// background element holding the slides
this.$myContainer = $('<div />').
addClass(secondPrivate);
// cache backgrounds for manipulation
this.$elementCache = $();
this.createChildren();
this.initChildren();
if (this.options.autoPlay) {
this.play();
}
};
MyComponent.prototype = {
'createChildren': function () {
var $this, that = this;
this.$children.each(function () {
$this = $(this);
// do something, this example makes no sense
that.$elementCache.push($this.parent());
});
},
'initChildren': function () {
$(firstPrivate).on('click', $.proxy(this.play, this));
},
'play': function (timeout) {
var that = this;
that.intervalId = window.setInterval(function() {
that.doSomething();
}, timeout || this.options.timeout);
}
};
$.fn.myComponent = function () {
var args = Array.prototype.slice.call(arguments);
return this.each(function () {
var $this = $(this),
myComponent = $this.data('myComponent');
if (!myComponent) {
$this.data('myComponent',
(myComponent = new MyComponent(this, args[0])));
}
if (!args.length || typeof args[0] === 'object') {
myComponent.play();
} else if (typeof args[0] === 'number') {
myComponent.play(args[0]);
}
});
};
})(window, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment