Skip to content

Instantly share code, notes, and snippets.

@nominalaeon
Last active October 17, 2016 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nominalaeon/c5411b7c90089ad72f51a02c4989c0a1 to your computer and use it in GitHub Desktop.
Save nominalaeon/c5411b7c90089ad72f51a02c4989c0a1 to your computer and use it in GitHub Desktop.
/**
* es5
* Class declaration, methods and properties assignments hoisted to the top
* Assignments returned from a function hidden below so the basic pattern for the class is always the same, always at the top
* Properties are put on the Class prototype as getter/setters
*/
/
function ClassName() {
this.init.apply(this, arguments);
}
_.extend(ClassName.prototype, getMethods());
Object.defineProperties(ClassName.prototype, getProperties());
return ClassName;
function getMethods() {
return {
init: function (seedObj) {
this._vm = {};
for (var prop in photo) {
if (!seedObj.hasOwnProperty(prop)) { continue; }
this[prop] = seedObj[prop];
}
},
getStuff: getStuff,
setStuff: setStuff
};
}
function getProperties() {
return {
id: {
get: function () {
return this._vm.id || null;
},
set: function (id) {
this._vm.id = id;
}
},
name: {
get: function () {
return this._vm.name || '';
},
set: function (name) {
this._vm.name = name;
}
},
stuff: {
get: function () {
return this._vm.stuff || [];
},
set: function (name) {
this._vm.name = stuff;
}
}
}
}
/**
* Methods
* Class methods reference functions so when these functions get lengthy, they don't jumble up the top.
* Method assignments above now act as a sort of Table of Contents for the forest of functions below.
*/
function getStuff() {
var promise = apiService.getStuff().promise;
return promise.then(setStuff.bind(this));
}
function setStuff(results) {
var stuff = [];
results.data.foreach(function (thing) {
var stuffVM = new stuffClass(thing);
stuff.push(stuffVM);
});
this.stuff = stuff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment