Skip to content

Instantly share code, notes, and snippets.

@cointilt
Created June 25, 2014 15:33
Show Gist options
  • Save cointilt/65e3b11ee370a3cd278f to your computer and use it in GitHub Desktop.
Save cointilt/65e3b11ee370a3cd278f to your computer and use it in GitHub Desktop.
AngularJS Model
// 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