Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
Fixed model bug when using single selection
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Jun 14, 2014
1 parent 5087990 commit feef54f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angularjs-dropdown-multiselect",
"version": "1.3.3",
"version": "1.3.4",
"authors": [
"Dotan Simha <dotansimha@gmail.com>"
],
Expand Down
2 changes: 1 addition & 1 deletion dist/angularjs-dropdown-multiselect.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pages/javascripts/pages/home/ExampleCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ angular.module('exampleApp').controller('ExampleCtrl', ['$scope', function($scop

$scope.example10settings = {selectionLimit: 2};

$scope.example12model = [];
$scope.example12model = {};
$scope.example12data = [
{id: 1, label: "David"},
{id: 2, label: "Jhon"},
Expand Down
2 changes: 1 addition & 1 deletion pages/javascripts/pages/home/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ <h3>Code</h3>
extra-settings="example10settings"></div>

// JavaScript
$scope.example12model = [];
$scope.example12model = {}; // ! IMPORTANT !
$scope.example12data = [
{id: 1, label: "David"},
{id: 2, label: "Jhon"},
Expand Down
32 changes: 20 additions & 12 deletions src/angularjs-dropdown-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,6 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co

$scope.singleSelection = $scope.settings.selectionLimit === 1;

if ($scope.singleSelection)
{
if (angular.isArray($scope.selectedModel) && $scope.selectedModel.length === 0)
{
$scope.selectedModel = null;
}
}

function getFindObj(id)
{
Expand All @@ -131,6 +124,21 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
return findObj;
}

function clearObject(object)
{
for (var prop in object) {
delete object[prop];
}
}

if ($scope.singleSelection)
{
if (angular.isArray($scope.selectedModel) && $scope.selectedModel.length === 0)
{
clearObject($scope.selectedModel);
}
}

if ($scope.settings.closeOnBlur) {
$document.on('click', function (e) {
var target = e.target.parentElement;
Expand Down Expand Up @@ -169,7 +177,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co

if ($scope.singleSelection)
{
totalSelected = $scope.selectedModel !== null ? 1 : 0;
totalSelected = ($scope.selectedModel !== null && angular.isDefined($scope.selectedModel[$scope.settings.idProp])) ? 1 : 0;
}
else
{
Expand Down Expand Up @@ -218,12 +226,11 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
}

if ($scope.singleSelection) {
$scope.selectedModel = null;
clearObject($scope.selectedModel);
}
else {
$scope.selectedModel.splice(0, $scope.selectedModel.length);
}

};

$scope.setSelectedItem = function(id, dontRemove){
Expand All @@ -239,7 +246,8 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co

if ($scope.singleSelection)
{
$scope.selectedModel = finalObj;
clearObject($scope.selectedModel);
angular.extend($scope.selectedModel, finalObj);
$scope.externalEvents.onItemSelect(finalObj);

return;
Expand All @@ -261,7 +269,7 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
$scope.isChecked = function (id) {
if ($scope.singleSelection)
{
return $scope.selectedModel !== null && $scope.selectedModel[$scope.settings.idProp] === getFindObj(id)[$scope.settings.idProp];
return $scope.selectedModel !== null && angular.isDefined($scope.selectedModel[$scope.settings.idProp]) && $scope.selectedModel[$scope.settings.idProp] === getFindObj(id)[$scope.settings.idProp];
}

return _.findIndex($scope.selectedModel, getFindObj(id)) !== -1;
Expand Down

0 comments on commit feef54f

Please sign in to comment.