Skip to content

Commit 426d81a

Browse files
authored
fix #48 correct pristine/dirty flags when provided initial data (#91)
1 parent ed14e2c commit 426d81a

File tree

8 files changed

+58
-18
lines changed

8 files changed

+58
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# CHANGELOG
22

3+
- Prevent $dirty flag on a form from being set to false on initialization
4+
35
## v1.0.0 - Major refactoring
46

57
The code has been rewritten almost from scratch with a focus on code quality and testing. This will make future development easier and limit the number of bugs on the long term. Beware that regressions are likely!

Gruntfile.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ module.exports = function (grunt) {
1414
]
1515
}
1616
},
17+
copy: {
18+
files: {
19+
expand: true,
20+
src: ['angular-typeahead.js', 'angular-typeahead.min.js'],
21+
cwd: 'build',
22+
dest: 'dist/'
23+
}
24+
},
1725
uglify: {
1826
build: {
19-
src: 'dist/angular-typeahead.js',
20-
dest: 'dist/angular-typeahead.min.js'
27+
src: 'build/angular-typeahead.js',
28+
dest: 'build/angular-typeahead.min.js'
2129
}
2230
},
2331
karma: {
@@ -50,7 +58,7 @@ module.exports = function (grunt) {
5058
src: {
5159
options: {
5260
src: 'angular-typeahead.js',
53-
dest: 'dist/angular-typeahead.js',
61+
dest: 'build/angular-typeahead.js',
5462
amdModuleId: 'angular-typeahead',
5563
deps: {
5664
default: ['angular'],
@@ -87,16 +95,22 @@ module.exports = function (grunt) {
8795

8896
// Load the plugins that provide the tasks.
8997
grunt.loadNpmTasks('grunt-contrib-clean');
98+
grunt.loadNpmTasks('grunt-contrib-copy');
9099
grunt.loadNpmTasks('grunt-contrib-uglify');
91100
grunt.loadNpmTasks('grunt-contrib-jshint');
92101
grunt.loadNpmTasks('grunt-karma');
93102
grunt.loadNpmTasks('grunt-contrib-watch');
94103
grunt.loadNpmTasks('grunt-umd');
95104

96-
grunt.registerTask('require-self', 'Sets-up requireself', require('./tasks/require-self'));
105+
grunt.registerTask('require-self', 'Fixes require calls to self for tests', require('./tasks/require-self'));
106+
107+
// Utility Tasks
108+
grunt.registerTask('_build', ['require-self', 'umd']);
109+
grunt.registerTask('_test', ['karma', 'jshint']);
110+
97111

98112
// Tasks
99-
grunt.registerTask('test:lite', ['require-self', 'karma:global', 'jshint']);
100-
grunt.registerTask('test', ['require-self', 'umd:test', 'karma', 'jshint']);
101-
grunt.registerTask('default', ['test', 'umd:src', 'uglify', 'clean']);
113+
grunt.registerTask('test:lite', ['_build', 'karma:global', 'jshint']);
114+
grunt.registerTask('test', ['_build', '_test']);
115+
grunt.registerTask('dist', ['_build', 'uglify', 'copy']);
102116
};

angular-typeahead.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ angular.module('siyfion.sfTypeahead', [])
101101
scope.$watch('options', initialize);
102102
initialized = true;
103103
} else {
104-
var value = element.val();
105104
$typeahead(element, 'destroy');
106105
$typeahead(element, options, datasets);
107-
ngModel.$setViewValue(value);
108106
}
109107
}
110108

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"scripts": {
1414
"test": "grunt test",
1515
"watch": "grunt watch",
16-
"dist": "grunt",
17-
"pretest": "require-self"
16+
"dist": "grunt"
1817
},
1918
"files": [
2019
"dist"
@@ -40,6 +39,7 @@
4039
"browserify": "^13.1.0",
4140
"grunt": "1.0.1",
4241
"grunt-contrib-clean": "~1.0.0",
42+
"grunt-contrib-copy": "^1.0.0",
4343
"grunt-contrib-jshint": "^1.0.0",
4444
"grunt-contrib-uglify": "~2.0.0",
4545
"grunt-contrib-watch": "^1.0.0",
@@ -56,7 +56,6 @@
5656
"karma-requirejs": "^1.1.0",
5757
"karma-spec-reporter": "0.0.26",
5858
"karma-threshold-reporter": "^0.1.15",
59-
"require-self": "^0.1.0",
6059
"requirejs": "^2.3.2",
6160
"typeahead.js": "^0.11.1",
6261
"watchify": "^3.7.0"

tasks/require-self.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ module.exports = function() {
66

77
// Get the name of the module in the current working directory.
88
var cwd = process.cwd();
9-
var pkg = require(path.join(cwd, 'package.json'));
10-
var name = pkg.name;
119

1210
// Compute the location and content for the pseudo-module.
13-
var modulePath = path.join(cwd, 'node_modules', name + '.js');
14-
var moduleText = "module.exports = require('..');";
11+
var modulePath = path.join(cwd, 'node_modules/angular-typeahead.js');
12+
var moduleText = "module.exports = require('../build/angular-typeahead.js');";
1513

1614
// Create the pseudo-module.
1715
fs.writeFileSync(modulePath, moduleText);

test/angular-typeahead.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ describe('sfTypeahead', function() {
5050
$provide.value('$typeahead', $typeahead);
5151
}));
5252

53+
describe('Behaviour in a form', function() {
54+
it('sets the proper form flags when there is data', inject(function($rootScope, $compile) {
55+
$scope = createScope($rootScope);
56+
$compile('<form name="theForm">' +
57+
'<input ng-model="model" type="text" options="options" datasets="datasets" required sf-typeahead>' +
58+
'</form>')($scope);
59+
$scope.$digest();
60+
expect($scope.theForm.$pristine).toEqual(true);
61+
expect($scope.theForm.$dirty).toEqual(false);
62+
expect($scope.theForm.$valid).toEqual(true);
63+
expect($scope.theForm.$invalid).toEqual(false);
64+
}));
65+
it('sets the proper form flags when there is no data', inject(function($rootScope, $compile) {
66+
$scope = createScope($rootScope);
67+
$scope.model = undefined;
68+
$compile('<form name="theForm">' +
69+
'<input ng-model="model" type="text" options="options" datasets="datasets" required sf-typeahead>' +
70+
'</form>')($scope);
71+
$scope.$digest();
72+
expect($scope.theForm.$pristine).toEqual(true);
73+
expect($scope.theForm.$dirty).toEqual(false);
74+
expect($scope.theForm.$valid).toEqual(false);
75+
expect($scope.theForm.$invalid).toEqual(true);
76+
}));
77+
});
5378
describe('Directive syntax', function() {
5479
it('is compiled on class name', inject(function($rootScope, $compile) {
5580
$scope = createScope($rootScope);
@@ -111,6 +136,7 @@ describe('sfTypeahead', function() {
111136
expect($typeahead).toHaveBeenCalledWith(jasmine.anything(), 'destroy');
112137
expect($typeahead).toHaveBeenCalledWith(jasmine.anything(), $scope.options, [$scope.datasets]);
113138
expect($element.val()).toEqual('simple value');
139+
expect($scope.model).toEqual('simple value');
114140
}));
115141
it('recreates the typeahead when datasets attribute changes',
116142
inject(function($rootScope, $compile) {
@@ -125,6 +151,7 @@ describe('sfTypeahead', function() {
125151
expect($typeahead).toHaveBeenCalledWith(jasmine.anything(), 'destroy');
126152
expect($typeahead).toHaveBeenCalledWith(jasmine.anything(), $scope.options, [$scope.datasets]);
127153
expect($element.val()).toEqual('simple value');
154+
expect($scope.model).toEqual('simple value');
128155
}));
129156
it('recreates the typeahead when datasets array attribute changes',
130157
inject(function($rootScope, $compile) {
@@ -140,6 +167,7 @@ describe('sfTypeahead', function() {
140167
expect($typeahead).toHaveBeenCalledWith(jasmine.anything(), 'destroy');
141168
expect($typeahead).toHaveBeenCalledWith(jasmine.anything(), $scope.options, $scope.datasets);
142169
expect($element.val()).toEqual('simple value');
170+
expect($scope.model).toEqual('simple value');
143171
}));
144172
it('recreates the typeahead when datasets array becomes a single dataset',
145173
inject(function($rootScope, $compile) {
@@ -155,6 +183,7 @@ describe('sfTypeahead', function() {
155183
expect($typeahead).toHaveBeenCalledWith(jasmine.anything(), 'destroy');
156184
expect($typeahead).toHaveBeenCalledWith(jasmine.anything(), $scope.options, [$scope.datasets]);
157185
expect($element.val()).toEqual('simple value');
186+
expect($scope.model).toEqual('simple value');
158187
}));
159188
});
160189
describe('model', function() {

test/karma.amd.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ conf.files = [
77
'node_modules/typeahead.js/dist/typeahead.bundle.js',
88
{pattern: 'node_modules/angular/angular.js', included: false},
99
{pattern: 'node_modules/angular-mocks/angular-mocks.js', included: false},
10-
{pattern: 'dist/angular-typeahead.js', included: false},
10+
{pattern: 'build/angular-typeahead.js', included: false},
1111
'build/angular-typeahead.spec.js'
1212
];
1313
conf.frameworks.push('requirejs');

test/test-amd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require.config({
2323
deps: allTestFiles,
2424

2525
paths: {
26-
'angular-typeahead': 'dist/angular-typeahead',
26+
'angular-typeahead': 'build/angular-typeahead',
2727
'typeahead': 'node_modules/typeahead.js/dist/typeahead.bundle',
2828
'angular': 'node_modules/angular/angular',
2929
'angular-mocks': 'node_modules/angular-mocks/angular-mocks',

0 commit comments

Comments
 (0)