cloudfour / SimpleSlideView

A nifty little jQuery or Zepto plugin for the simplest of sliding views.
http://blog.cloudfour.com/simpleslideview/
258 stars 33 forks source link

Should return jQuery obj (this) #11

Closed lyzadanger closed 10 years ago

lyzadanger commented 10 years ago

Right now, we are doing this:

  $.fn.simpleSlideView = function(options, extras) {
    options = options || {};

    if (typeof options !== 'object') {
      options = {
        views: options
      };
    } else {
      $.extend(options, extras);
    }
    return new SimpleSlideView(this, options);
  };

This breaks the metaphor of jQuery plugins, which is to return this, i.e. the jQuery object. The way the plugin is currently architected, instantiation of a SimpleSlideView is necessary. I don't want to re-design the object, but may look at returning this instead of the SimpleSlideView object. This would help with encapsulation, also, as presently one could use the object returned to mess around with a bunch of internals.

tylersticka commented 10 years ago

I agree. What I will say is that it's super important that we be able to retrieve or otherwise manipulate the SimpleSlideView object because it's important to advanced use-cases to be able to call methods like changeView directly.

The issue I ran into last time was that Zepto does not ship with the data method by default. Now that we won't be limited by that, how I might approach this would be to create the SimpleSlideView object, with $.fn.simpleSlideView instantiating the object and attaching it as a data value to the element(s) ($(el).data('simpleSlideView', slideView)).

What I might also do is create a $.simpleSlideView method that accepted an element as the first object and returned the SimpleSlideView object. This would give the user two ways of grabbing that object...

// Method 1
var slideView = $.simpleSlideView(el);
// Method 2
var slideView = $(el).simpleSlideView().data('simpleSlideView');

Just some ideas! :)