Forked from greyaperez/boilerplate-angular.domain-model.js
Created
December 20, 2016 21:29
-
-
Save burre/ebb4aedfb3d9d56739aa2c67119442dd 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 <tim.adam.perez@gmail.com> | |
| */ | |
| '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