I have developed the following jQuery code.
I have some questions:
- What is the best programming pattern to develop a jQuery plugin?
- How do I convert my code into a plugin? (I know it is possible to do what I am asking with
$.fn.function, but what I don't understand is how to use it with an object.) - Should I learn and master a single programming pattern or do I have to learn patterns, all exists?
Here is my HTML:
<ul class="linkContainer">
<li><a href="#" class="link" data-link="one">one</a></li>
<li><a href="#" class="link" data-link="two">two</a></li>
<div class="moreDetailsSection" data-section="one">
chakabakas
</div>
<div class="moreDetailsSection" data-section="two">
sasjakjk
</div>
And my JavaScript:
(function ($) {
var animation = {
options: {
linkContainer : $('.linkContainer'),
section: $('.section'),
link : $('.link'),
mainEffect: 'slideToggle',
mainEffectSpeed: 300
},
init: function (options) {
var link = this.options.link;
$.extend(this.options, options);
link.on('click', this.changeWindow );
},
changeWindow: function () {
var linkId = $(this).data('link'),
options = animation.options,
sectionContainer = animation.options.section,
elements = sectionContainer,
element = elements.filter('[data-section="'+ linkId +'"]');
element[options.mainEffect](options.mainEffectSpeed);
}
}
animation.init();
})(jQuery);