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

onSelect callback #279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
64 changes: 24 additions & 40 deletions src/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,28 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec

// Enable watching of the options dataset if in use
if (tElm.is('select')) {
repeatOption = tElm.find( 'optgroup[ng-repeat], optgroup[data-ng-repeat], option[ng-repeat], option[data-ng-repeat]');
repeatOption = tElm.find('option[ng-repeat], option[data-ng-repeat]');

if (repeatOption.length) {
repeatAttr = repeatOption.attr('ng-repeat') || repeatOption.attr('data-ng-repeat');
watch = jQuery.trim(repeatAttr.split('|')[0]).split(' ').pop();
}
}
var hasOnSelect = true;

return function (scope, elm, attrs, controller) {
// instance-specific options
var opts = angular.extend({}, options, scope.$eval(attrs.uiSelect2));

//@edited for free text plus custom setting of value assigned to ng-model, jrustia
if (!opts.onSelect) {
opts.onSelect = function (data) { return data; };
hasOnSelect = false;
}

/*
Convert from Select2 view-model to Angular view-model.
*/
Convert from Select2 view-model to Angular view-model.
*/
var convertToAngularModel = function(select2_data) {
var model;
if (opts.simple_tags) {
Expand All @@ -51,8 +58,8 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
};

/*
Convert from Angular view-model to Select2 view-model.
*/
Convert from Angular view-model to Select2 view-model.
*/
var convertToSelect2Model = function(angular_data) {
var model = [];
if (!angular_data) {
Expand Down Expand Up @@ -82,7 +89,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec

if (controller) {
// Watch the model for programmatic changes
scope.$watch(tAttrs.ngModel, function(current, old) {
scope.$watch(tAttrs.ngModel, function (current, old) {
if (!current) {
return;
}
Expand All @@ -96,32 +103,19 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
elm.select2('val', controller.$viewValue);
} else {
if (opts.multiple) {
controller.$isEmpty = function (value) {
return !value || value.length === 0;
};
var viewValue = controller.$viewValue;
if (angular.isString(viewValue)) {
viewValue = viewValue.split(',');
}
elm.select2(
'data', convertToSelect2Model(viewValue));
if (opts.sortable) {
elm.select2("container").find("ul.select2-choices").sortable({
containment: 'parent',
start: function () {
elm.select2("onSortStart");
},
update: function () {
elm.select2("onSortEnd");
elm.trigger('change');
}
});
}
} else {
if (angular.isObject(controller.$viewValue)) {
elm.select2('data', controller.$viewValue);
} else if (!controller.$viewValue) {
elm.select2('data', null);
} else if (hasOnSelect) {
//TODO don't know yet what to do
} else {
elm.select2('val', controller.$viewValue);
}
Expand All @@ -139,7 +133,7 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
$timeout(function () {
elm.select2('val', controller.$viewValue);
// Refresh angular to remove the superfluous option
controller.$render();
elm.trigger('change');
if(newVal && !oldVal && controller.$setPristine) {
controller.$setPristine(true);
}
Expand All @@ -161,30 +155,26 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
});

if (!isSelect) {

// Set the view and model value and update the angular template manually for the ajax/multiple select2.
elm.bind("change", function (e) {
e.stopImmediatePropagation();

if (scope.$$phase || scope.$root.$$phase) {
return;
}
scope.$apply(function () {
controller.$setViewValue(
convertToAngularModel(elm.select2('data')));
opts.onSelect(convertToAngularModel(elm.select2('data'))));
});
});

if (opts.initSelection) {
var initSelection = opts.initSelection;
opts.initSelection = function (element, callback) {
initSelection(element, function (value) {
var isPristine = controller.$pristine;
controller.$setViewValue(convertToAngularModel(value));
controller.$setViewValue(opts.onSelect(convertToAngularModel(value)));
callback(value);
if (isPristine) {
controller.$setPristine();
}
elm.prev().toggleClass('ng-pristine', controller.$pristine);
});
};
}
Expand Down Expand Up @@ -215,21 +205,15 @@ angular.module('ui.select2', []).value('uiSelect2Config', {}).directive('uiSelec
elm.select2(opts);

// Set initial value - I'm not sure about this but it seems to need to be there
elm.select2('data', controller.$modelValue);
elm.val(controller.$viewValue);
// important!
controller.$render();

// Not sure if I should just check for !isSelect OR if I should check for 'tags' key
if (!opts.initSelection && !isSelect) {
var isPristine = controller.$pristine;
controller.$pristine = false;
controller.$setViewValue(
convertToAngularModel(elm.select2('data'))
);
if (isPristine) {
controller.$setPristine();
}
elm.prev().toggleClass('ng-pristine', controller.$pristine);
controller.$setViewValue(
opts.onSelect(convertToAngularModel(elm.select2('data')))
);
}
});
};
Expand Down