Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

ui-select2 set dirty flag after initialization when simple_tags mode is on #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
});
}

/*
Set view value, set pristine state on select2 element and restore pristine state
if it was set before we call controller.$setViewValue
*/
var setInitialViewValue = function (angularModel) {
var parentForm = elm.inheritedData('$formController');
var wasParentFormPristine = parentForm ? parentForm.$pristine : null;

controller.$setViewValue(angularModel);

controller.$setPristine();
// restore pristine state
if (wasParentFormPristine) {
parentForm.$setPristine();
}
};

// Initialize the plugin late so that the injected DOM does not disrupt the template compiler
$timeout(function () {
elm.select2(opts);
Expand All @@ -185,9 +202,9 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
controller.$render();

// Not sure if I should just check for !isSelect OR if I should check for 'tags' key
if (!opts.initSelection && !isSelect)
controller.$setViewValue(
convertToAngularModel(elm.select2('data')));
if (!opts.initSelection && !isSelect) {
setInitialViewValue(convertToAngularModel(elm.select2('data')));
}
});
};
}
Expand Down
11 changes: 10 additions & 1 deletion test/select2Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,16 @@ describe('uiSelect2', function () {
expect(scope.foo).toEqual(['tag1', 'tag2']);
});

});
it('should return pristine state to true', function () {
scope.foo = ['tag1', 'tag2'];

compile('<form name="testForm"><input name="testElement" ng-model="foo" ui-select2="options"></form>');
scope.$digest();

expect(scope.testForm.$pristine).toBeTruthy();
expect(scope.testForm.testElement.$pristine).toBeTruthy();
});

});
});
});