1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Multiple widget instances sharing data

Discussion in 'jQuery' started by jsbenson, Nov 1, 2013.

  1. #1
    Hey everyone. I'm trying to create multiple instances of a widget I'm creating but the variables that should be contained in each instance is being shared across all of them.
    widget:
    ;(function($,window,document,undefined){
    var $this,
    $el,
    $url,
    $btnClose;
    $.widget("dev.smt",{
    options: {
    windowID:"",
    title:"",
    winTrans:50
    },
    _create:function(){
    $this=this,
    $el=$this.element;
    var $markup="\
    <div class='smt_window' id='"+$this.options.windowID+"' style='display:none;'>\
    <a id='"+$this.options.windowID+"_btnClose' href='#'><img class='close' src='img/close.png'/></a>\
    <p class='hdr unSelectable'>"+$this.options.title+"</p>\
    </div>";
    $("body").append($markup);
    $win=$("#"+$this.options.windowID);
    $btnClose=$($this.options.windowID+"_btnClose");
    $el.click(function(e){$this.display(true); });
    $btnClose.click(function(e){$this.display(false);});
    },
    display:function(aDisplay){
    if(aDisplay){
    this.win.fadeIn(50);
    } else {
    this.win.fadeOut(50);
    }
    }
    });
    })(jQuery,window,document);
    -----------------------
    elements:
    $("#SMT_btnUserRegistry").smt({
    windowID:"smt_userRegistry",
    title:"User Registry"
    });
    $("#SMT_btnAnnouncements").smt({
    windowID:"smt_announcements",
    title:"Announcements"
    });
    $("#SMT_btnPictureManager").smt({
    windowID:"smt_pictureManager",
    title:"Picture Manager"
    });
    --------------------------
    $this and the other vars that should be local to the function are getting stepped on by each instance created. Still familiarizing myself with plugins and widgets. Any guidance is greatly appreciated.
     
    jsbenson, Nov 1, 2013 IP
  2. jsbenson

    jsbenson Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    Figured it out. $this and other vars were global to all instances of the widgets. Brought them down a level and all was fixed.
     
    jsbenson, Nov 2, 2013 IP