Fixed: First item is always selected if selected binding value is set before content binding value.

Test case is provided.
This commit is contained in:
Martin Carlberg committed 2016-04-05 23:22:29 +02:00
1 parent 598a6ebaed
commit 699503042f
2 files changed
+53 -3

No files matched your search

+23 -3
View File
@@ -847,7 +847,17 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down");
{
var count = [aValue count],
options = [_info objectForKey:CPOptionsKey],
offset = [self _getInsertNullOffset];
offset = [self _getInsertNullOffset],
selectedBindingInfo = [_source infoForBinding:CPSelectedObjectBinding],
selectedObject = nil;
if (selectedBindingInfo)
{
var destination = [selectedBindingInfo objectForKey:CPObservedObjectKey],
keyPath = [selectedBindingInfo objectForKey:CPObservedKeyPathKey];
selectedObject = [destination valueForKeyPath:keyPath];
}
if (count + offset != [_source numberOfItems])
{
@@ -858,9 +868,19 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down");
for (var i = 0; i < count; i++)
{
var item = [[CPMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:nil];
[self _setValue:[aValue objectAtIndex:i] forItem:item withOptions:options];
var item = [[CPMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:nil],
itemValue = [aValue objectAtIndex:i];
[self _setValue:itemValue forItem:item withOptions:options];
[_source addItem:item];
// Select this item if it is the one selected by the selected object binding
// This is needed if the selected object binding is set before the items
// from the content binding
if (itemValue === selectedObject)
{
[_source setSelectedIndex:[_source numberOfItems] - 1];
}
}
}
else
+30
View File
@@ -324,6 +324,36 @@
[self assert:2 equals:[[self button] indexOfSelectedItem]];
}
- (void)testSimpleObjectBindingArrayControllerWithLateContent
{
var arrayController = [[CPArrayController alloc] init],
objectController = [[CPObjectController alloc] init],
testObject = [CPMutableDictionary dictionary],
martin = [CPDictionary dictionaryWithJSObject:{@"name": @"Martin"}],
malte = [CPDictionary dictionaryWithJSObject:{@"name": @"Malte"}],
johan = [CPDictionary dictionaryWithJSObject:{@"name": @"Johan"}],
menuObjects = [martin, malte, johan];
[testObject setObject:@"I'm a testObject" forKey:@"Who am I"];
[objectController setContent:testObject];
[button bind:CPContentBinding toObject:arrayController withKeyPath:@"arrangedObjects" options:nil];
[button bind:CPContentValuesBinding toObject:arrayController withKeyPath:@"arrangedObjects.name" options:nil];
[button bind:CPSelectedObjectBinding toObject:objectController withKeyPath:@"selection.xxx" options:nil];
[testObject setObject:martin forKey:@"xxx"];
[arrayController setContent:menuObjects];
[self assert:0 equals:[[self button] indexOfSelectedItem]];
[arrayController setContent:nil];
[testObject setObject:malte forKey:@"xxx"];
[arrayController setContent:menuObjects];
[self assert:1 equals:[[self button] indexOfSelectedItem]];
[arrayController setContent:nil];
[testObject setObject:johan forKey:@"xxx"];
[arrayController setContent:menuObjects];
[self assert:2 equals:[[self button] indexOfSelectedItem]];
}
- (void)testObjectBindingNullPlaceholderOption
{
var arrayController = [[CPArrayController alloc] init],