Skip to content

Commit

Permalink
fixed #9 Besides that, all bootstrap-select options is watched.
Browse files Browse the repository at this point in the history
  • Loading branch information
lordfriend committed Jul 4, 2014
1 parent 669b4a6 commit f151601
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/nya-bootstrap-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ angular.module('nya.bootstrap.select',[])
// prevent selectDirective render an unknownOption.
selectCtrl.renderUnknownOption = angular.noop;
var optionArray = [];

// store data- attribute options of select
var selectorOptions = {};
var BS_ATTR = ['container', 'countSelectedText', 'dropupAuto', 'header', 'hideDisabled', 'selectedTextFormat', 'size', 'showSubtext', 'showIcon', 'showContent', 'style', 'title', 'width', 'disabled'];

var checkSelectorOptionsEquality = function() {
var isEqual = true;
angular.forEach(BS_ATTR, function(attr) {
isEqual = isEqual && attrs[attr] === selectorOptions[attr];
});
};

var updateSelectorOptions = function() {
angular.forEach(BS_ATTR, function(attr) {
selectorOptions[attr] = attrs[attr];
});
};

/**
* Check option data attributes, text and value equality.
* @param opt the option dom element
Expand Down Expand Up @@ -88,6 +106,10 @@ angular.module('nya.bootstrap.select',[])
if(hasChanged) {
buildSelector();
}
if(!checkSelectorOptionsEquality()) {
updateSelectorOptions();
$(element).selectpicker('refresh');
}
optionArray = makeOptionArray(optionElements);
}

Expand Down
6 changes: 6 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ beforeEach(function() {
};

return this.actual.hasClass(cls);
},
toHaveAttribute: function(cls) {
this.message = function() {
return "Expected '" + angular.mock.dump(this.actual) + "' to have attribute '" + cls + "'.";
};
return this.actual.is('['+cls+']');
}
});
});
50 changes: 50 additions & 0 deletions test/spec/nya-bootstrap-select-special.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

/**
* Test for special scenario
*/

describe('test for some special scenario', function() {
var $scope, $compile, rootElement;

var options = ['Alpha', 'Bravo', 'Charlie', 'Delta',
'Echo', 'Foxtrot', 'Golf', 'Hotel', 'Juliet', 'Kilo', 'Lima',
'Mike', 'November', 'Oscar', 'Papa', 'Quebec', 'Romeo', 'Sierra',
'Tango', 'Uniform', 'Victor', 'Whiskey', 'X-ray', 'Yankee', 'Zulu'
];

// load the nyaBootstrapSelect module
beforeEach(module('nya.bootstrap.select'));

beforeEach(inject(function(_$rootScope_, _$compile_){
$scope = _$rootScope_.$new();
$compile = _$compile_;
}));

it('should refresh selector when some attribute changed', function() {
$scope.options = options;
$scope.enableSelector = true;
$scope.$digest();

rootElement = $compile('<div class="select-container">' +
'<select class="nya-selectpicker" ng-model="myModel" ng-disabled="!enableSelector">' +
'<option ng-repeat="option in options" value="{{option}}">{{option}}</option>' +
'</select>' +
'</div>')($scope);

$scope.$digest();

var selectElement = rootElement.children('.nya-selectpicker');
var selectButton = selectElement.next().children('.dropdown-toggle');

expect(selectElement).not.toHaveAttribute('disabled');
expect(selectButton).not.toHaveClass('disabled');

$scope.enableSelector = false;

$scope.$digest();

expect(selectElement).toHaveAttribute('disabled');
expect(selectButton).toHaveClass('disabled');
});
});

0 comments on commit f151601

Please sign in to comment.