Skip to content

Instantly share code, notes, and snippets.

@maccman
Created February 11, 2011 13:56
Show Gist options
  • Save maccman/822371 to your computer and use it in GitHub Desktop.
Save maccman/822371 to your computer and use it in GitHub Desktop.
if (typeof Object.create !== "function")
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
var Model = {
prototype: {
init: function(){}
},
create: function(){
return Object.create(this);
},
inst: function(){
var instance = Object.create(this.prototype);
instance.init.apply(instance, arguments);
return instance;
}
};
jQuery.extend(Model, {
find: function(){},
});
jQuery.extend(Model.prototype, {
load: function(attributes){
for(var name in attributes)
this[name] = attributes[name];
}
});
var Asset = Model.create("Asset");
Asset.prototype.init = function(){
console.log('loaded')
};
var asset = Asset.inst();
@weepy
Copy link

weepy commented Feb 11, 2011

here's a similar model i'm using :

var extend = require("./utils").extend
var inherits = require("./utils").inherits

var Base = function() {}
Base.prototype.initialize = function() {}
Base.prototype.loaded = function() {}
Base.prototype.destroy = function() {}
Base.prototype.toData = function() {
  var o = {klass: this.klass}, properties = this.constructor.properties
  for(var i in properties) {
    if(this[i] !== properties[i])
      o[i] = this[i]
  }
  return o
}

function Obj(){}
Obj.klasses = {}
Obj.klass = function(klass, properties, parent) {
  parent = parent || Base
  properties = extend(parent.properties, properties)
  var cnstr = Obj.klasses[klass] = function(data) {
    this.id = data.id
    for(var i in properties) {
      var datum = data[i]
      this[i] = datum === undefined ? properties[i] : datum
    }
    this.klass = klass  
    this.initialize()
  }  
  inherits(cnstr, parent)
  cnstr.properties = properties
  // cnstr.prototype.klass = klass
  // cnstr.klass = klass
  return cnstr
}

module.exports = Obj

@weepy
Copy link

weepy commented Feb 11, 2011

here's a similar model i'm using :

var extend = require("./utils").extend
var inherits = require("./utils").inherits

var Base = function() {}
Base.prototype.initialize = function() {}
Base.prototype.loaded = function() {}
Base.prototype.destroy = function() {}
Base.prototype.toData = function() {
  var o = {klass: this.klass}, properties = this.constructor.properties
  for(var i in properties) {
    if(this[i] !== properties[i])
      o[i] = this[i]
  }
  return o
}

function Obj(){}
Obj.klasses = {}
Obj.klass = function(klass, properties, parent) {
  parent = parent || Base
  properties = extend(parent.properties, properties)
  var cnstr = Obj.klasses[klass] = function(data) {
    this.id = data.id
    for(var i in properties) {
      var datum = data[i]
      this[i] = datum === undefined ? properties[i] : datum
    }
    this.klass = klass  
    this.initialize()
  }  
  inherits(cnstr, parent)
  cnstr.properties = properties
  // cnstr.prototype.klass = klass
  // cnstr.klass = klass
  return cnstr
}

module.exports = Obj

@weepy
Copy link

weepy commented Feb 11, 2011

function inherits(child, parent) {
  var ctor = function(){};
  ctor.prototype = parent.prototype;
  child.prototype = new ctor();
  child.prototype.constructor = child;
  child.__super__ = parent.prototype;
}

var Model = function(klass, parent) {
  var cnstr = function() { 
    this.init.apply(this, arguments)
  }
  cnstr.prototype.init = function() {} 
  if(parent) {
     inherits(cnstr, parent)
     for(var i in parent) cnstr[i] = parent[i] 
  }

  return cnstr
}

var Asset = Model("Asset");
  
Asset.prototype.init = function(){
  console.log('loaded')
};
  
var asset = new Asset();

@maccman
Copy link
Author

maccman commented Feb 11, 2011

var SuperClass = function(parent){
  var klass = function(){
    this.init.apply(this, arguments);
  };

  // Change klass's prototype 
  if (parent) {
    var subclass = function() { };
    subclass.prototype = parent.prototype;
    klass.prototype = new subclass;
  };

  klass.prototype.init = function(){};

  // Shortcuts
  klass.fn = klass.prototype;
  klass.fn._class = klass;
  klass._super = klass.__proto__;      

  /* include/extend code... */

  return klass;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment