Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

feat(ngModel): bind to single property instead of entire object #107

Merged
merged 4 commits into from
Jul 29, 2014
Merged
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
"karma-ng-html2js-preprocessor": "^0.1.0",
"karma-phantomjs-launcher": "~0.1.4"
},
"scripts": {
"postinstall": "bower install",
"test": "gulp test"
},
"license": "MIT"
}
96 changes: 55 additions & 41 deletions src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,40 @@
* Original discussion about parsing "repeat" attribute instead of fully relying on ng-repeat:
* https://github.com/angular-ui/ui-select/commit/5dd63ad#commitcomment-5504697
*/
.service('RepeatParser', ['uiSelectMinErr', function(uiSelectMinErr) {
.service('RepeatParser', ['uiSelectMinErr','$parse', function(uiSelectMinErr, $parse) {
var self = this;

/**
* Example:
* expression = "address in addresses | filter: {street: $select.search} track by $index"
* lhs = "address",
* rhs = "addresses | filter: {street: $select.search}",
* itemName = "address",
* source = "addresses | filter: {street: $select.search}",
* trackByExp = "$index",
* valueIdentifier = "address",
* keyIdentifier = undefined
*/
self.parse = function(expression) {
if (!expression) {
throw uiSelectMinErr('repeat', "Expected 'repeat' expression.");
}

var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);
var match = expression.match(/^\s*(?:([\s\S]+?)\s+as\s+)?([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);

if (!match) {
throw uiSelectMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",
expression);
}

var lhs = match[1]; // Left-hand side
var rhs = match[2]; // Right-hand side
var trackByExp = match[3];

match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);
if (!match) {
throw uiSelectMinErr('iidexp', "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.",
lhs);
expression);
}

// Unused for now
// var valueIdentifier = match[3] || match[1];
// var keyIdentifier = match[2];

return {
lhs: lhs,
rhs: rhs,
trackByExp: trackByExp
itemName: match[2], // (lhs) Left-hand side,
source: match[3], // (rhs) Right-hand side,
trackByExp: match[4],
modelMapper: $parse(match[1] || match[2])
};

};

self.getGroupNgRepeatExpression = function() {
return '($group, $items) in $select.groups';
};

self.getNgRepeatExpression = function(lhs, rhs, trackByExp, grouped) {
var expression = lhs + ' in ' + (grouped ? '$items' : rhs);
self.getNgRepeatExpression = function(itemName, source, trackByExp, grouped) {
var expression = itemName + ' in ' + (grouped ? '$items' : source);
if (trackByExp) {
expression += ' track by ' + trackByExp;
}
Expand Down Expand Up @@ -180,14 +163,15 @@
ctrl.items = items;
}

var repeat = RepeatParser.parse(repeatAttr),
setItemsFn = groupByExp ? updateGroups : setPlainItems;
var setItemsFn = groupByExp ? updateGroups : setPlainItems;

ctrl.parserResult = RepeatParser.parse(repeatAttr);

ctrl.isGrouped = !!groupByExp;
ctrl.itemProperty = repeat.lhs;
ctrl.itemProperty = ctrl.parserResult.itemName;

// See https://github.com/angular/angular.js/blob/v1.2.15/src/ng/directive/ngRepeat.js#L259
$scope.$watchCollection(repeat.rhs, function(items) {
$scope.$watchCollection(ctrl.parserResult.source, function(items) {

if (items === undefined || items === null) {
// If the user specifies undefined or null => reset the collection
Expand All @@ -204,6 +188,7 @@
}

});

};

var _refreshDelayPromise;
Expand Down Expand Up @@ -355,6 +340,32 @@
var $select = ctrls[0];
var ngModel = ctrls[1];

//From view --> model
ngModel.$parsers.unshift(function (inputValue) {
var locals = {};
locals[$select.parserResult.itemName] = inputValue;
var result = $select.parserResult.modelMapper(scope, locals);
return result;
});

//From model --> view
ngModel.$formatters.unshift(function (inputValue) {
var match = $select.parserResult.source.match(/^\s*([\S]+).*$/);
var data = scope[match[1]];
if (data){
for (var i = data.length - 1; i >= 0; i--) {
var locals = {};
locals[$select.parserResult.itemName] = data[i];
var result = $select.parserResult.modelMapper(scope, locals);
if (result == inputValue){
return data[i];
}
}
}
return inputValue;
});


//Idea from: https://github.com/ivaynberg/select2/blob/79b5bf6db918d7560bdd959109b7bcfb47edaf43/select2.js#L1954
var focusser = angular.element("<input ng-disabled='$select.disabled' class='ui-select-focusser ui-select-offscreen' type='text' aria-haspopup='true' role='button' />");
$compile(focusser)(scope);
Expand Down Expand Up @@ -533,9 +544,15 @@
},

compile: function(tElement, tAttrs) {
var repeat = RepeatParser.parse(tAttrs.repeat);
var groupByExp = tAttrs.groupBy;

if (!tAttrs.repeat) throw uiSelectMinErr('repeat', "Expected 'repeat' expression.");

return function link(scope, element, attrs, $select, transcludeFn) {

// var repeat = RepeatParser.parse(attrs.repeat);
var groupByExp = attrs.groupBy;

$select.parseRepeatAttr(attrs.repeat, groupByExp); //Result ready at $select.parserResult

if(groupByExp) {
var groups = element.querySelectorAll('.ui-select-choices-group');
Expand All @@ -548,10 +565,9 @@
throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row but got '{0}'.", choices.length);
}

choices.attr('ng-repeat', RepeatParser.getNgRepeatExpression(repeat.lhs, '$select.items', repeat.trackByExp, groupByExp))
.attr('ng-mouseenter', '$select.setActiveItem('+repeat.lhs+')')
.attr('ng-click', '$select.select(' + repeat.lhs + ')');

choices.attr('ng-repeat', RepeatParser.getNgRepeatExpression($select.parserResult.itemName, '$select.items', $select.parserResult.trackByExp, groupByExp))
.attr('ng-mouseenter', '$select.setActiveItem('+$select.parserResult.itemName +')')
.attr('ng-click', '$select.select(' + $select.parserResult.itemName + ')');

transcludeFn(function(clone) {
var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner');
Expand All @@ -562,8 +578,6 @@
$compile(element)(scope);
});

$select.parseRepeatAttr(attrs.repeat, groupByExp);

scope.$watch('$select.search', function() {
$select.activeIndex = 0;
$select.refresh(attrs.refresh);
Expand Down
107 changes: 97 additions & 10 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('ui-select tests', function() {
$rootScope = _$rootScope_;
scope = $rootScope.$new();
$compile = _$compile_;

scope.selection = {}
scope.getGroupLabel = function(person) {
return person.age % 2 ? 'even' : 'odd';
};
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('ui-select tests', function() {
}

return compileTemplate(
'<ui-select ng-model="selection"' + attrsHtml + '> \
'<ui-select ng-model="selection.selected"' + attrsHtml + '> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('ui-select tests', function() {
});

it('should correctly render initial state', function() {
scope.selection = scope.people[0];
scope.selection.selected = scope.people[0];

var el = createUiSelect();

Expand Down Expand Up @@ -178,7 +178,7 @@ describe('ui-select tests', function() {
scope.items = ['false'];

var el = compileTemplate(
'<ui-select ng-model="selection"> \
'<ui-select ng-model="selection.selected"> \
<ui-select-match>{{$select.selected}}</ui-select-match> \
<ui-select-choices repeat="item in items | filter: $select.search"> \
<div ng-bind-html="item | highlight: $select.search"></div> \
Expand All @@ -199,7 +199,7 @@ describe('ui-select tests', function() {
}
function createUiSelect() {
return compileTemplate(
'<ui-select ng-model="selection"> \
'<ui-select ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices group-by="\'group\'" repeat="person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
Expand Down Expand Up @@ -249,7 +249,7 @@ describe('ui-select tests', function() {
describe('choices group by function', function() {
function createUiSelect() {
return compileTemplate(
'<ui-select ng-model="selection"> \
'<ui-select ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices group-by="getGroupLabel" repeat="person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
Expand All @@ -268,7 +268,7 @@ describe('ui-select tests', function() {
it('should throw when no ui-select-choices found', function() {
expect(function() {
compileTemplate(
'<ui-select ng-model="selection"> \
'<ui-select ng-model="selection.selected"> \
<ui-select-match></ui-select-match> \
</ui-select>'
);
Expand All @@ -278,7 +278,7 @@ describe('ui-select tests', function() {
it('should throw when no repeat attribute is provided to ui-select-choices', function() {
expect(function() {
compileTemplate(
'<ui-select ng-model="selection"> \
'<ui-select ng-model="selection.selected"> \
<ui-select-choices></ui-select-choices> \
</ui-select>'
);
Expand All @@ -288,9 +288,96 @@ describe('ui-select tests', function() {
it('should throw when no ui-select-match found', function() {
expect(function() {
compileTemplate(
'<ui-select ng-model="selection"> \
'<ui-select ng-model="selection.selected"> \
<ui-select-choices repeat="item in items"></ui-select-choices> \
</ui-select>'
);
}).toThrow(new Error('[ui.select:transcluded] Expected 1 .ui-select-match but got \'0\'.'));
});});
});

it('should format the model correctly using alias', function() {
var el = compileTemplate(
'<ui-select ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person as person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);
clickItem(el, 'Samantha');
expect(scope.selection.selected).toBe(scope.people[5]);
});

it('should parse the model correctly using alias', function() {
var el = compileTemplate(
'<ui-select ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person as person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);
scope.selection.selected = scope.people[5];
scope.$digest();
expect(getMatchLabel(el)).toEqual('Samantha');
});

it('should format the model correctly using property of alias', function() {
var el = compileTemplate(
'<ui-select ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person.name as person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);
clickItem(el, 'Samantha');
expect(scope.selection.selected).toBe('Samantha');
});

it('should parse the model correctly using property of alias', function() {
var el = compileTemplate(
'<ui-select ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person.name as person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);
scope.selection.selected = 'Samantha';
scope.$digest();
expect(getMatchLabel(el)).toEqual('Samantha');
});

it('should parse the model correctly using property of alias but passed whole object', function() {
var el = compileTemplate(
'<ui-select ng-model="selection.selected"> \
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
<ui-select-choices repeat="person.name as person in people | filter: $select.search"> \
<div ng-bind-html="person.name | highlight: $select.search"></div> \
<div ng-bind-html="person.email | highlight: $select.search"></div> \
</ui-select-choices> \
</ui-select>'
);
scope.selection.selected = scope.people[5];
scope.$digest();
expect(getMatchLabel(el)).toEqual('Samantha');
});

it('should format the model correctly without alias', function() {
var el = createUiSelect();
clickItem(el, 'Samantha');
expect(scope.selection.selected).toBe(scope.people[5]);
});

it('should parse the model correctly without alias', function() {
var el = createUiSelect();
scope.selection.selected = scope.people[5];
scope.$digest();
expect(getMatchLabel(el)).toEqual('Samantha');
});
});