Created
June 25, 2014 15:33
-
-
Save cointilt/65e3b11ee370a3cd278f to your computer and use it in GitHub Desktop.
AngularJS Model
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
// Creating a fake model called Unit | |
(function (angular) { | |
'use strict'; | |
// Unit Model Construtor | |
function Unit (futureUnitData) { | |
// Data is immediately available | |
if (! futureUnitData.inspect) { | |
// not sure if this is underscore, LoDash or something with angular | |
// need to research | |
_.extend(this, futureUnitData); | |
return; | |
} | |
// the promis will be unwrapped first | |
this.$unwrap(futureUnitData); | |
} | |
// The factory to be used with angular | |
Unit.$factory = ['$tiemout', function ($timeout) { | |
_.extend(Unit, { | |
$timeout: $timeout | |
}); | |
return Unit; | |
}]; | |
// Register factory with angular | |
angular.module('nsModel').factory('nsUnit', Unit.$factory); | |
// Promise UnWrap Method | |
Unit.prototype.$unwrap = function (futureUnitData) { | |
var self = this; | |
this.$futureUnitData = futureUnitData; | |
this.$futureUnitData.then(function (data) { | |
if (! self.$valid(data)) { | |
return new Error('Trouble in Paradise'); | |
} | |
Unit.$timeout(function () [ | |
_.extend(self, data); | |
]); | |
}); | |
} | |
// Public methods | |
Unit.prototype.get = function (key) { | |
return; | |
}; | |
Unit.prototype.set = function (key, val) { | |
return; | |
}; | |
// Private function | |
function checkRole (role) { | |
return; | |
} | |
// Private property | |
var defaultRoles = ['user', 'admin']; | |
})(angular); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment