Skip to content

Instantly share code, notes, and snippets.

@travisperson
Created September 22, 2011 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save travisperson/1235688 to your computer and use it in GitHub Desktop.
Save travisperson/1235688 to your computer and use it in GitHub Desktop.
Overwriting a function prototype
var util = require("util");
//// Model ////
var Model = function(data) {
this._data = data
}
Model.prototype.show = function (data, cb) {
// act on data and then return exicute the callback
// like pulling something from the database
cb(this._data[data.user])
}
/// Collections Model
var collectionModel = function (data) {
Model.call(this, data)
}
util.inherits(collectionModel, Model)
/// Extending the model
collectionModel.prototype.show = function (data, cb) {
var _show = this.show
_show(data, function (data) { // TypeError: undefined is not a function
data.name = data.name.toUpperCase();
cb(data);
})
}
var data = [
{
name: 'Travis'
}
, {
name: 'John'
}
, {
name: 'Sarah'
}
]
//
// General model
//
model = new Model(data)
model.show({
user: 1
}, function (data) {
console.dir(data)
})
//
// Collection model, returns name in uppercase
//
collection = new collectionModel(data)
collection.show({
user: 1
}, function (data) {
console.dir(data)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment