Skip to content

Commit

Permalink
Fix DateField watches
Browse files Browse the repository at this point in the history
  • Loading branch information
Kmaschta committed Mar 8, 2017
1 parent 51a50a3 commit ad7287b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ transpile:
cp -Rf ./src/sass/ lib/sass/

build:
rm -rf build/*
make transpile
NODE_ENV=production ./node_modules/webpack/bin/webpack.js -p --optimize-minimize --optimize-occurence-order --optimize-dedupe --progress --devtool source-map
cp -Rf build examples/blog/
Expand All @@ -33,6 +34,9 @@ check-only-in-tests:
test-unit:
./node_modules/.bin/karma start src/javascripts/test/karma.conf.js --single-run

test-unit-watch:
./node_modules/.bin/karma start src/javascripts/test/karma.conf.js --watch

test-e2e: prepare-test-e2e
./node_modules/.bin/protractor src/javascripts/test/protractor.conf.js

Expand Down
31 changes: 25 additions & 6 deletions src/javascripts/ng-admin/Crud/field/maDateField.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const valueToRawValue = (value) => {
if (value instanceof Date) {
return value;
}

return new Date(value);
};

/**
* Edition field for a date - a text input with a datepicker.
*
Expand All @@ -13,23 +21,34 @@ export default function maDateField() {
link: function(scope, element) {
var field = scope.field();
scope.name = field.name();
scope.rawValue = scope.value == null ? null : (scope.value instanceof Date ? scope.value : new Date(scope.value));
scope.rawValue = scope.value == null ? null : valueToRawValue(scope.value);

scope.$watch('rawValue', function(newValue) {
scope.value = field.parse()(newValue);
scope.$watch('rawValue', function(newRawValue) {
const newValue = field.parse()(newRawValue);

if (!angular.equals(scope.value, newValue)) {
scope.value = newValue;
}
});

scope.$watch('value', (newValue, oldValue) => {
if (newValue === oldValue) {
if (angular.equals(newValue, oldValue)) {
return;
}

if (!newValue) {
scope.rawValue = null;
if (scope.rawValue !== null) {
scope.rawValue = null;
}

return;
}

scope.rawValue = scope.value instanceof Date ? scope.value : new Date(scope.value);
const newRawValue = valueToRawValue(scope.value);

if (!angular.equals(scope.rawValue, newRawValue)) {
scope.rawValue = newRawValue;
}
});

scope.format = field.format();
Expand Down
14 changes: 14 additions & 0 deletions src/javascripts/test/unit/Crud/field/maDateFieldSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,18 @@ describe('directive: date-field', function() {

expect(element.find('input').eq(0).val()).toBe('2010-01-01');
});

it('should update value when updating the rawValue directly', () => {
const now = new Date('2016-09-18');
scope.value = now;
scope.field = new DateField();

const element = $compile(directiveUsage)(scope);
const isolatedScope = element.isolateScope();

isolatedScope.rawValue = new Date('2010-01-01');
isolatedScope.$digest();

expect(isolatedScope.value).toBe('2010-01-01');
});
});

0 comments on commit ad7287b

Please sign in to comment.