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

Commit

Permalink
fix(select): allow circular references
Browse files Browse the repository at this point in the history
closes #5330, references #3268
  • Loading branch information
rschmukler committed Nov 4, 2015
1 parent bb83931 commit cba5fa7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,11 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
// and values matching every option's controller.
self.options = {};

$scope.$watch(function() {
$scope.$watchCollection(function() {
return self.options;
}, function() {
self.ngModel.$render();
}, true);
});

var deregisterCollectionWatch;
var defaultIsEmpty;
Expand Down Expand Up @@ -629,6 +629,7 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
}
}
self.ngModel.$setViewValue(self.isMultiple ? values : values[0]);
self.ngModel.$render();
};

function renderMultiple() {
Expand Down
16 changes: 12 additions & 4 deletions src/components/select/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ describe('<md-select>', function() {
expect(selectedOptions(el).length).toBe(0);
});

it('supports circular references', function() {
var opts = [{ id: 1 }, { id: 2 }];
opts[0].refs = opts[1];
opts[1].refs = opts[0];
setup('ng-model="$root.model"', opts, { renderValueAs: 'value.id' });
});

it('renders model change by selecting new and deselecting old', inject(function($rootScope) {
$rootScope.$apply('model = "b"');
var el = setup('ng-model="$root.model"', ['a','b','c']);
Expand Down Expand Up @@ -767,10 +774,10 @@ describe('<md-select>', function() {
return el;
}

function setup(attrs, options) {
function setup(attrs, options, compileOpts) {
var el;
inject(function($compile, $rootScope) {
var optionsTpl = optTemplate(options);
var optionsTpl = optTemplate(options, compileOpts);
var fullTpl = '<md-select-menu ' + (attrs || '') + '>' + optionsTpl +
'</md-select-menu>';
el = $compile(fullTpl)($rootScope);
Expand All @@ -786,12 +793,13 @@ describe('<md-select>', function() {
return setup(attrs, options);
}

function optTemplate(options) {
function optTemplate(options, compileOpts) {
var optionsTpl = '';
inject(function($rootScope) {
if (angular.isArray(options)) {
$rootScope.$$values = options;
optionsTpl = '<md-option ng-repeat="value in $$values" ng-value="value">{{value}}</md-option>';
var renderValueAs = compileOpts ? compileOpts.renderValueAs || 'value' : 'value';
optionsTpl = '<md-option ng-repeat="value in $$values" ng-value="value">{{' + renderValueAs + '}}</md-option>';
} else if (angular.isString(options)) {
optionsTpl = options;
}
Expand Down

0 comments on commit cba5fa7

Please sign in to comment.