Sha256: 0aba0c9cca8637a2ee41b1d6a0d5e40c18a13b0b02663406a16e7f333f561bd9

Contents?: true

Size: 1.64 KB

Versions: 40

Compression:

Stored size: 1.64 KB

Contents

var Skull = { View: undefined };
if (typeof exports !== 'undefined') {
  Skull = exports;
} 

Skull.View = Backbone.View.extend({
  addView: function(name, view){
    if(!this['_' + name]){
      this['_' + name] = new view({name: name, parent: this});
    }
    return this['_' + name];
  },

  initialize: function(options){
    _.bindAll(this);
    if(options){
      this.name = options.name;
      this.parent = options.parent;
    }

    if(_.isFunction(this.beforeActivate)){
      this.beforeActivate();
    }

    this.createViewGetters();

    if(_.isFunction(this.activate)){
      this.activate();
    }
  },

  navigate: function(url){
    window.location.href = url;
  },

  // Create a getter to initilize each view defined in the constructor when needed
  createViewGetters: function(){
    _.each(Object.getPrototypeOf(this).constructor.views, function(val, key){
      var name = key[0].toLowerCase() + key.substring(1);
      Object.defineProperty(this, name, {
        configurable: true,
        enumerable: true,
        get: function(){
          return this.addView(name, val);
        }
      });
      // Initialize the view if the el is present in the parent's DOM subtree
      if(this.$(val.el).length > 0) this[name];
    }, this);
  }
},
// Static methods
{
  // We just overwrite the extend to extract the el property and store it in the constructor
  // That's how we look for the view's el before initializing it
  views: {},
  addChild: function(name, protoProps, staticProps){
    var child = Skull.View.extend(protoProps, _.extend({views: {}}, staticProps));
    child.el = protoProps.el;
    this.views[name] = child;
    return this;
  }
});

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
catarse_moip-3.2.0 spec/javascripts/support/skull.js
catarse_moip-3.1.1 spec/javascripts/support/skull.js
catarse_moip-3.1.0 spec/javascripts/support/skull.js
catarse_moip-2.4.0 spec/javascripts/support/skull.js
catarse_moip-3.0.5 spec/javascripts/support/skull.js
catarse_moip-3.0.4 spec/javascripts/support/skull.js
catarse_moip-3.0.3 spec/javascripts/support/skull.js
catarse_moip-3.0.2 spec/javascripts/support/skull.js
catarse_moip-3.0.1 spec/javascripts/support/skull.js
catarse_moip-3.0.0 spec/javascripts/support/skull.js
catarse_moip-2.3.6 spec/javascripts/support/skull.js
catarse_moip-2.3.5 spec/javascripts/support/skull.js
catarse_moip-2.3.4 spec/javascripts/support/skull.js
catarse_moip-2.3.3 spec/javascripts/support/skull.js
catarse_moip-2.3.2 spec/javascripts/support/skull.js
catarse_moip-2.3.1 spec/javascripts/support/skull.js
catarse_moip-2.3.0 spec/javascripts/support/skull.js
catarse_moip-2.2.1 spec/javascripts/support/skull.js
catarse_moip-2.2.0 spec/javascripts/support/skull.js
catarse_moip-2.1.9 spec/javascripts/support/skull.js