bmorwood / chaos

a flex / flash to HTML5 boiler plate
0 stars 1 forks source link

presenter class needs a super initializer created. #6

Open bmorwood opened 10 years ago

bmorwood commented 10 years ago
            p.$initialize = function (){
                this.setModel(this.model);
                this.modelBound = true;

                if(this.events){
                    var self = this;
                    this.events.forEach(function(e){
                        maybeso.Emitter.on(e.name, self[e.callback], self);
                    });
                }
            };

            p.initialize = function(){
                this.$initialize();
            };
xpressivecode commented 10 years ago

Here is an interesting snippet that shows how to include super class methods:

function Class() { }
Class.prototype.construct = function() {};
Class.extend = function(def)
{
    var classDef = function()
    {
        if (arguments[0] !== Class)
        {
            this.construct.apply(this, arguments);
        }
    };
    var proto = new this(Class);
    var superClass = this.prototype;
    for (var n in def)
    {
        var item = def[n];                      
        if (item instanceof Function) item.$ = superClass;
        proto[n] = item;
    }
    classDef.prototype = proto;
    classDef.extend = this.extend;      
    return classDef;
};

so now you could say

//Hey, this class definition approach
//looks much cleaner than then others.
var BaseClass = Class.extend({
  construct: function() { /* optional constructor method */ },

  getName: function() {
      return "BaseClass(" + this.getId() + ")";
  },

  getId: function() {
      return 1;
  }
});

var SubClass = BaseClass.extend({
  getName: function() {
      //Calls the getName() method of BaseClass
      return "SubClass(" + this.getId() + ") extends " +
          arguments.callee.$.getName.call(this);
  },

  getId: function() {
      return 2;
  }
});

here is the article, the final version is down at the bottom

http://joshgertzen.com/object-oriented-super-class-method-calling-with-javascript/

this approach is nice as the method names don't change, just the accessor. and they are automatically hooked up!