Skip to content

Storing instance references

Nic Bell edited this page Aug 17, 2015 · 5 revisions

###Native

<div data-attach="Carousel" data-carousel-options='{ "width": 100 }'>
..
</div>
Attach.add('Carousel', function(el){
  el['Carousel'] = el['Carousel'] || new Carousel(el);
});

You may want to access instance later, this is easy because it is stored against the element.

var carousel = el['Carousel'];
carousel.showNext();

###jQuery

<div data-attach="Carousel" data-carousel-options='{ "width": 100 }'>
..
</div>
Attach.add('Carousel', function(el){
  if (!$(el).data('Carousel'))
    $(el).data('Carousel', new Carousel($(el), $(el).data('carousel-options')));
});

You may want to access instance later, this is easy because it is stored against the element.

var carousel = $(el).data('Carousel');
carousel.showNext();

###MooTools

<div data-attach="Carousel" data-carousel-options="{'width': 100}">
..
</div>
Attach.add('Carousel', function(el){
  if (el.retrieve('Carousel') === null)
    el.store('Carousel', new Carousel(el, JSON.decode(el.get('data-carousel-options'))));
});
window.addEvent('domready', function () {
  Attach.run();
});

You may want to access instance later, this is easy because it is stored against the element.

var carousel = el.retrieve('Carousel');
carousel.showNext();