Passing an Array as a Config Option in jQuery plugin
Hi,
I have posted this question on stack overflow to help me understand how to pass an array of objects in my plugin config option, but I have had no reply in the last 3 days. Would it be possible to know how I can pass an array inside my default config settings to allow users to add as many images as they like depending on the type of "array or parameter" that is provided.
The reason I am using arrays is depending on the type of array (desktop_xl[] , desktop_l[] , ipad_p[] , mobile_l[], mobile_p[] ), the user will have the option to add as many images as they like inside different div containers. Please see below my plugin (my arrays are not connected with the default options yet.., thanks very much in advance for any help or advises:
- ;(function($, window, document, undefined){
- //define MyjQueryPlugin object with default config settings:
-
- $.MyjQueryPlugin = {
- defaults: {
- imagecontainer: "#container",
- version: "v01",
- transitionfx: "fade" // "fade", "scrollHorz", "flipHorz", "shuffle", "tileSlide"
- // add my Arrays to default options here?
- // arrays should allow users to add as many images to #container div as they require
- // arrays are desktop_xl[] , desktop_l[] , ipad_p[] , mobile_l[], mobile_p[]
- }
- };
-
- //extend jquery with the plugin
- $.fn.extend({
- MyjQueryPlugin:function(config) {
-
- //use defaults or properties supplied by user
- var config = $.extend({}, $.MyjQueryPlugin.defaults, config );
-
- //append slides
- $(config.imagecontainer).append('<div class="cycle-slideshow slides-xld" data-cycle-fx="'+ config.transitionfx +'" data-cycle-timeout="0" data-cycle-log="false" data-cycle-pager="#pagerxld" data-cycle-center-horz="true" data-cycle-auto-height="container"></div><div class="cycle-slideshow slides-ld" data-cycle-fx="'+ config.transitionfx +'" data-cycle-timeout="0" data-cycle-log="false" data-cycle-pager="#pagerld" data-cycle-center-horz="true" data-cycle-auto-height="container"></div><div class="cycle-slideshow slides-ipadp" data-cycle-fx="'+ config.transitionfx +'" data-cycle-timeout="0" data-cycle-log="false" data-cycle-pager="#pageripadp" data-cycle-center-horz="true" data-cycle-auto-height="container"></div><div class="cycle-slideshow slides-mobilel" data-cycle-fx="'+ config.transitionfx +'" data-cycle-timeout="0" data-cycle-log="false" data-cycle-pager="#pagermobilel" data-cycle-center-horz="true" data-cycle-auto-height="container"></div><div class="cycle-slideshow slides-mobilep" data-cycle-fx="'+ config.transitionfx +'" data-cycle-timeout="0" data-cycle-log="false" data-cycle-pager="#pagermobilep" data-cycle-center-horz="true" data-cycle-auto-height="container"></div>').css('height', $(window).height() );
-
- // append MyjQueryPlugin sidebar
- this.append( '<div id="Mysidebar" class="open">' +
- '<p class="review">Version ' + config.version + '- ready for review</p>'+
- '<hr>' +
- '</div>')
- .children()
- .css('height', $(window).height() );
-
-
- //create array of objects
- var desktop_xl = [
- {
- "title":"Homepage", // text for sidebar
- "src":"slides/1200/Homepage.jpg"// path to image >= 1200px wide
- },
- {
- "title":"Categories", // text for sidebar
- "src":"slides/1200/Categories.jpg"// path to image >= 1200px wide
- },
- {
- "title":"Product description", // text for sidebar
- "src":"slides/1200/Product_description.jpg" // path to image >= 1200px wide
- }
- ];
-
- var desktop_l = [
- // if array is empty, remove elements from the page
- ];
-
- var ipad_p = [
- {
- "title":"Homepage", // text for sidebar
- "src":"slides/480/Homepage.jpg" // path to image >= 480px wide
- }
- ];
- var mobile_l = [];
- var mobile_p = [];
-
- // set Global Variables
- var width = $(window).width();
- var currHeight = $(window).height();
- var ctrl = $(".ctrl");
- var ulscreenlia = $('ul.screen li a');
- var sidebarlia = $('#MyjQueryPluginsidebar li a');
- var sidebar = $("#MyjQueryPluginsidebar");
- var ulscreenli = $('ul.screen li');
- if (desktop_xl.length === 0) {
- ulscreenli.eq(0).hide();
- $('div.select_join option[value="xld"]').remove();
- }
- if (desktop_l.length === 0) {
- ulscreenli.eq(1).hide();
- $('div.select_join option[value="ld"]').remove();
- }
- if (ipad_p.length === 0) {
- ulscreenli.eq(2).hide();
- $('div.select_join option[value="ip"]').remove();
- }
- if (mobile_l.length === 0) {
- ulscreenli.eq(3).hide();
- $('div.select_join option[value="ml"]').remove();
- }
- if (mobile_p.length === 0) {
- ulscreenli.eq(4).hide();
- $('div.select_join option[value="mp"]').remove();
- }
-
- //create img desktop_xl loop
- $.each(desktop_xl, function( index , value ){
-
- $('#container .slides-xld').append(
- //getting values from array but cannot understand how to pass array(s): desktop_xl, desktop_l, ipad_p, mobile_l, mobile_p inside config option
- //And Each arrays should allow user to add multiple images to #container dive
- $("<img />").attr({
- id: value.title,
- src: value.src,
- title: value.title
- })
-
-
- );
- });
-
- //create img ipadp loop
- $.each(ipad_p, function( index , value){
-
- $('#container .slides-ipadp').append(
-
- $("<img />").attr({
- id: value.title,
- src: value.src,
- title: value.title
- })
- );
- });
-
-
- function rundateobject(){
-
- var current_date = new Date ( );
-
- var month_names = new Array ( );
- month_names[month_names.length] = "January";
- month_names[month_names.length] = "February";
- month_names[month_names.length] = "March";
- month_names[month_names.length] = "April";
- month_names[month_names.length] = "May";
- month_names[month_names.length] = "June";
- month_names[month_names.length] = "July";
- month_names[month_names.length] = "August";
- month_names[month_names.length] = "September";
- month_names[month_names.length] = "October";
- month_names[month_names.length] = "November";
- month_names[month_names.length] = "December";
-
- var day_names = new Array ( );
- day_names[day_names.length] = "Sunday";
- day_names[day_names.length] = "Monday";
- day_names[day_names.length] = "Tuesday";
- day_names[day_names.length] = "Wednesday";
- day_names[day_names.length] = "Thursday";
- day_names[day_names.length] = "Friday";
- day_names[day_names.length] = "Saturday";
-
- $('#date').html( day_names[current_date.getDay()]
- + ', '
- + month_names[current_date.getMonth()]
- + ' '
- + current_date.getDate()
- + ' '
- + current_date.getFullYear() );
- };
- //create animation for anchor links with jQuery DOM ready function
- $(function(){
- $('a').hover(function(){
- $(this).animate({
- 'margin-left':10,
- 'padding-left':20
-
- },200);
- $(this).dequeue();
- },
- function() {
- $(this).animate({
- 'margin-left':0,
- 'padding-left':15
- },200);
- $(this).dequeue();
- }
- );
- });
-
- //on resize browser, adjust elements height
-
-
- //initialise plugins
- $(".nano").nanoScroller();
-
- //initialise functions
- rundateobject();
-
- //return the jquery object for chaining
- return this;
-
- }// config options
-
- }); // jQuery extend
-
-
- })(jQuery, window, document);