Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #145 from baleato/feature/am-from
Browse files Browse the repository at this point in the history
Added am-from attribute
  • Loading branch information
urish committed Jul 14, 2015
2 parents 859b76f + a62099d commit f1d4090
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 15 additions & 1 deletion angular-moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,14 @@
var localDate = new Date().getTime();
var preprocess = angularMomentConfig.preprocess;
var modelName = attr.amTimeAgo;
var currentFrom;
var isTimeElement = ('TIME' === element[0].nodeName.toUpperCase());

function getNow() {
var now;
if (amTimeAgoConfig.serverTime) {
if (currentFrom) {
now = currentFrom;
} else if (amTimeAgoConfig.serverTime) {
var localNow = new Date().getTime();
var nowMillis = localNow - localDate + amTimeAgoConfig.serverTime;
now = moment(nowMillis);
Expand Down Expand Up @@ -256,6 +259,17 @@
updateMoment();
});

if (angular.isDefined(attr.amFrom)) {
scope.$watch(attr.amFrom, function (value) {
if ((typeof value === 'undefined') || (value === null) || (value === '')) {
currentFrom = null;
} else {
currentFrom = moment(value);
}
updateMoment();
});
}

if (angular.isDefined(attr.amWithoutSuffix)) {
scope.$watch(attr.amWithoutSuffix, function (value) {
if (typeof value === 'boolean') {
Expand Down
11 changes: 11 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,17 @@ describe('module angularMoment', function () {
expect(element.text()).toBe('2013-12-15');
});
});

describe('am-from attribute', function() {
it('should make the calculations from the am-from given', function() {
$rootScope.from = new Date(2015, 6, 11);
$rootScope.testDate = new Date(2015, 6, 12);
var element = angular.element('<span am-time-ago="testDate" am-from="from"></span>');
element = $compile(element)($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('in a day');
});
});
});

describe('am-without-suffix attribute', function () {
Expand Down

0 comments on commit f1d4090

Please sign in to comment.