Created
September 18, 2013 16:46
-
-
Save gmilby/6611969 to your computer and use it in GitHub Desktop.
angular - create unique id
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
angular | |
.module('uuidApp', ['lvl.services']) | |
.controller('uuidCtl', ['$scope', 'uuid', function($scope, uuid){ | |
$scope.generateUuid = function() { | |
$scope.new = uuid.new(); | |
$scope.nInfo = new Date(); | |
}; | |
$scope.showEmpty = function() { | |
$scope.empty = uuid.empty(); | |
$scope.eInfo = new Date(); | |
}; | |
}]); | |
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
<html> | |
<head> | |
<title>UUID Service Demo</title> | |
<script src="script/lib/angular.js"></script> | |
<script src="script/lvl-uuid.js"></script> | |
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"> | |
</head> | |
<body ng-app="uuidApp"> | |
<h1>UUID Service</h1> | |
<p>A very simple service for working with UUIDs</p> | |
<div ng-controller='uuidCtl'> | |
<div class="row"> | |
<div class="col-md-4 col-md-offset-2"> | |
{{new}} | |
<br/> | |
<small>{{nInfo | date:'mediumDate'}} {{nInfo | date:'mediumTime'}}</small> | |
<br/> | |
<button class="btn btn-primary" ng-click='generateUuid()'>Generate UUID</button> | |
</div> | |
<div class="col-md-6"> | |
{{empty}} | |
<br/> | |
<small>{{eInfo | date:'mediumDate'}} {{eInfo | date:'mediumTime'}}</small> | |
<br/> | |
<button class="btn btn-primary" ng-click='showEmpty()'>Show empty UUID</button> | |
</div> | |
</div> | |
</div> | |
<h3>Page script</h3> | |
<pre> | |
angular | |
.module('uuidApp', ['lvl.services']) | |
.controller('uuidCtl', ['$scope', 'uuid', function($scope, uuid){ | |
$scope.generateUuid = function() { | |
$scope.new = uuid.new(); | |
$scope.nInfo = new Date(); | |
}; | |
$scope.showEmpty = function() { | |
$scope.empty = uuid.empty(); | |
$scope.eInfo = new Date(); | |
}; | |
}]); | |
</pre> | |
<script> | |
angular | |
.module('uuidApp', ['lvl.services']) | |
.controller('uuidCtl', ['$scope', 'uuid', function($scope, uuid){ | |
$scope.generateUuid = function() { | |
$scope.new = uuid.new(); | |
$scope.nInfo = new Date(); | |
}; | |
$scope.showEmpty = function() { | |
$scope.empty = uuid.empty(); | |
$scope.eInfo = new Date(); | |
}; | |
}]); | |
</script> | |
</body> | |
</html> |
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
var module; | |
try { | |
module = angular.module('lvl.services'); | |
} catch (e) { | |
module = angular.module('lvl.services', []); | |
} | |
module.factory('uuid', function() { | |
var svc = { | |
new: function() { | |
function _p8(s) { | |
var p = (Math.random().toString(16)+"000000000").substr(2,8); | |
return s ? "-" + p.substr(0,4) + "-" + p.substr(4,4) : p ; | |
} | |
return _p8() + _p8(true) + _p8(true) + _p8(); | |
}, | |
empty: function() { | |
return '00000000-0000-0000-0000-000000000000'; | |
} | |
}; | |
return svc; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment