Created
April 7, 2016 20:59
-
-
Save greyaperez/50bd1f9108a1ea1c63b10e987ac12a57 to your computer and use it in GitHub Desktop.
Angular - Domain Model Objects
This file contains hidden or 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
/** | |
* Domain Model: Boilerplate Example for Angular | |
* @description Class Model through Composition (looser coupling, higher cohesion) | |
* @note $cacheFactory mimics proper inheritance through uniquely indexed references | |
* @author Timothy A. Perez <[email protected]> | |
*/ | |
'use strict'; | |
(function () { | |
angular | |
.module('AppName') | |
.factory('Boilerplate', BoilerplateModel); | |
BoilerplateModel.$inject = ['$cacheFactory','$log']; | |
function BoilerplateModel ($cacheFactory, $log) { | |
// @note 's' plural | |
var cache = $cacheFactory('Boilerplates'); | |
function Boilerplate (id, msg) { | |
// @note Must pass uniq identifier to cache | |
cache.put(this.id, this); | |
this.id = id || null; | |
this.msg = msg || null; | |
} | |
//////////////// | |
// Instance Methods | |
//////////////// | |
/** | |
* example setter | |
*/ | |
Boilerplate.prototype.storeMsg = function (msg) { | |
this.msg = msg; | |
}; | |
/** | |
* example getter | |
*/ | |
Boilerplate.prototype.getId = function () { | |
return this.id; | |
}; | |
//////////////// | |
// Static Methods | |
//////////////// | |
/******** | |
* @note Regarding Chained Properties | |
* You could also chain child elements, during construct methods, | |
* through composition by invoking the .construct() method of an | |
* $injected model to ensure nodes in a tree of data is | |
* utilizing models. | |
* @see "example 001" | |
*/ | |
/** | |
* Object Factory | |
* @description constructor from Config Object | |
*/ | |
Boilerplate.construct = function (data) { | |
// @example 001: data.user = User.construct(data.user) | |
return cache.get(data.id) || new Boilerplate(data.id, data.msg); | |
} | |
/** | |
* Object Factory from JSON Object | |
* @description constructor from JSON Data | |
*/ | |
Boilerplate.constructFromJSON = function (data) { | |
// TODO | |
// - JSON Validation | |
// - JSON Data Mapping | |
// @example 001: data.user = User.construct(data.User) | |
return cache.get(data.id) || new Boilerplate(data.id, data.msg); | |
} | |
/** | |
* example static method | |
*/ | |
Boilerplate.getTimestamp = function () { | |
return new Date() / 1; | |
} | |
return Boilerplate; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment