Skip to content

Instantly share code, notes, and snippets.

@bmhaskar
Last active August 29, 2015 14:11
Show Gist options
  • Save bmhaskar/c533ea65eca725bbcfb7 to your computer and use it in GitHub Desktop.
Save bmhaskar/c533ea65eca725bbcfb7 to your computer and use it in GitHub Desktop.
Backbone like data-modelling
(
function(root,factory){
root.Buzz = factory(root, (function(){}) , root.$ );
}(this,
function(root, Buzz,$) {
var array = [];
var slice = array.slice;
var idCounter = 0;
var _ = function(obj) {
if(obj instanceof _) return obj;
if (!(this instanceof _)) return new _(obj);
this._wrapped = obj;
}
__ = _(root);
_.prototype.isFunction = function(obj) {
return typeof obj == 'function' || false;
};
_.prototype.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
_.prototype.has = function(obj, key) {
return obj != null && hasOwnProperty.call(obj, key);
};
_.prototype.uniqueId = function(prefix) {
var id = ++idCounter + '';
return prefix ? prefix + id : id;
};
_.prototype.result = function(object, property) {
if (object == null) return void 0;
var value = object[property];
return __.isFunction(value) ? object[property]() : value;
};
_.prototype.isObject = function(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
_.prototype.defaults = function(obj) {
if (!__.isObject(obj)) return obj;
for (var i = 1, length = arguments.length; i < length; i++) {
var source = arguments[i];
for (var prop in source) {
if (obj[prop] === void 0) obj[prop] = source[prop];
}
}
return obj;
};
_.prototype.extend = function() {
var consumer = arguments[0],
providers = slice.call(arguments, 1),
key,
i,
provider;
for (i = 0; i < providers.length; ++i) {
provider = providers[i];
for (key in provider) {
if (hasOwnProperty.call(provider,key)) {
consumer[key] = provider[key];
};
};
};
return consumer;
}
Buzz.$ = $;
var Model = Buzz.Model = function(attributes) {
var obj = function(instanceAttr) {
//Create local variable attrs and set it to attributes passed
var attrs = instanceAttr || {};
//Create unique cid for model
this.cid = __.uniqueId('c');
attrs = __.defaults({}, attrs, __.result(attributes, 'defaults'));
this.attributes = attrs;
};
obj.prototype = Object.create(Model.prototype);
obj.prototype.constructor = Model;
__.extend(obj.prototype,attributes);
obj.extend = extend;
return obj;
}
Model.prototype = {
get: function(attr) {
return this.attributes[attr];
}
};
var extend = function(protoProps, staticProps) {
var parent = this;
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && __.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = parent.prototype.constructor.apply(this,arguments) ;
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = parent;
}
// Add static properties to the constructor function, if supplied.
__.extend(child, parent, staticProps);
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) __.extend(child.prototype, protoProps);
return child;
};
return Buzz;
}
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment