Sha256: 1a0b99e850e5f810cb407b751f6d1672c9d6ce2323d66ce12f2c7a782553a95a

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

//     Emerson Sink
//
//     Adds...

(function(ns) {

  // Emerson Extension
  // --------------------------------------------------------------------------

  // ### Emerson.sink module
  var define = ns.sink = function(view) {};

  // ### Module API
  //   * `ns` is a reference to the namespace.
  //   * `init` is a hook for initializing the module.
  _.extend(define, {
    ns     : ns,
    init   : function init() {}
  });


  // "Base" Libary Extension
  // --------------------------------------------------------------------------

  // Make a local copy of Emerson.base. e.g., one of jQuery, Zepto or Ender.
  var $ = ns.base;

  // ### $.fn.sink
  //
  //     $(target).sink()
  //
  // Given "replacement" content and for each "sink" (existing content):
  //
  //   1. fire "sink:before" with the sink as the target.
  //   2. replace the sink with a "prepared" replacement.
  //   3. fire "sink:after" with the replacement as the target.
  $.fn.sink = function() {
    _.each(this, function(e) {
      var elem = $(e);
      var key  = elem.data('sink');

      if(key) {
        $('[data-sink="' + key + '"]').each(function() {
          $(this)
            .trigger('sink:before')
            .replaceWith(prepare(elem));
        });

        $('[data-sink="' + key + '"]').trigger('sink:after');
      }
    });

    return this;
  };


  // Internal Implementation
  // --------------------------------------------------------------------------

  // ### prepare
  // Clone the replacement source and, if Emerson.view is defined, apply that.
  function prepare(source) {
    var result = source.clone();

    if(Emerson.view) {
      result.view();
    }

    return result;
  }
})(Emerson);

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
emerson-0.0.5 vendor/assets/javascripts/emerson/sink.js