Created
September 30, 2014 16:35
-
-
Save dholmes/00a4f9b45a7ad9a9301a to your computer and use it in GitHub Desktop.
JS Inheritance the wrong way (apparently)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Still trying to get the handle on JS OO - WAY too many years in C++ style languages I guess | |
// Anyway, for looking at later | |
function Collection() { | |
this.nextNewId = 1; | |
this.count = 0; | |
this.data = {}; | |
} | |
Collection.prototype.getAll = function(){ | |
return this.data; | |
} | |
Collection.prototype.get = function(id){ | |
$.each( this.data, function( key, item ) { | |
if(key == id) return item; | |
}); | |
return undefined; | |
} | |
Collection.prototype.edit = function(id,item){ | |
this.data[id] = item; | |
$(this).trigger('collection-edit',[id,item]); | |
return this.data; | |
} | |
Collection.prototype.add = function(item){ | |
debugger; | |
var id = this.nextNewId; | |
var tempId = "new-"+id; | |
this.data[tempId] = item; | |
$(this).trigger('collection-add',[item,tempId]); | |
return this.data; | |
} | |
Collection.prototype.delete = function(id){ | |
$(this).trigger('collection-delete',[id]); | |
return this.data; | |
} | |
function TagsCollection() { | |
this.judgePermissions = {}; | |
Collection.call(); | |
$(this).on('collection-add',function(item, tempId){ | |
console.dir(item); | |
}); | |
}; | |
TagsCollection.prototype = Object.create(Collection.prototype); | |
TagsCollection.prototype.constructor = TagsCollection; | |
TagsCollection.prototype.refresh = function() { | |
$.ajax('/competition-judging-setup/list-permissions?format=json',{ | |
'dataType': 'json', | |
context:this | |
}).done(function(data){ | |
this.data = data.data; | |
if(typeof callback == 'function') callback(); | |
}) | |
}; | |
TagsCollection.prototype.dump = function(){ | |
console.dir(this.data); | |
} | |
var tags = new TagsCollection(); | |
tags.refresh(); | |
//tags.add({'item':"foo"}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment