Replacing object in arraycontroller with binding

32 views
Skip to first unread message

Arjen Schipmolder

unread,
Nov 4, 2015, 1:26:14 PM11/4/15
to objec...@googlegroups.com
Hi all,

I’ve been running into a problem with array controllers and bindings for a while now and am not sure what the best approach is to prevent this.

The main question is how you should replace an object within an arraycontroller’s content without it breaking the bindings to properties of the object.

Imaging an array controller that manages a number of custom objects of type “Product".
The array controller itself is the source (via bindings) for a tableview and the tableview in turn displays the custom “price" property of “Product” (also with bindings).

That all works fine, no problems at all, but the problems start when i need to update the product object that’s currently selected (for example, to update all properties).
I now can replace the object directly in the array controller’s content array using replaceObjectAtIndex:withObject: but that doesn’t refresh the array controller.
I can also add the object to the array controller itself but that leaves the old object still in there and is also complex keeping the correct product selected.

Does anyone have any pointers in the official way to do this as I’m currently working from one work around to the next.

Thanks!

daboe01

unread,
Nov 4, 2015, 1:34:25 PM11/4/15
to Cappuccino & Objective-J
instead of replacing the object, you could update all of its properties using the appropriate accessors.

Aparajita

unread,
Nov 4, 2015, 2:29:16 PM11/4/15
to Cappuccino & Objective-J
Using [object setValue:newValue forKey:@"someProperty"] *should* update the object in the tableview.

Arjen Schipmolder

unread,
Nov 4, 2015, 3:32:00 PM11/4/15
to objec...@googlegroups.com
Thanks a lot guys.

The [object setValue:newValue forKey:@"someProperty”] does indeed work well for the object’s properties.
This way I can now also update properties of objects contained within an array which in turn is a property of the original “Product” object so this is great.

All I need to do now is rewrite few update methods but that shouldn’t be an issue.

Thanks!
Arjen
--
You received this message because you are subscribed to the Google Groups "Cappuccino & Objective-J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectivej+...@googlegroups.com.
To post to this group, send email to objec...@googlegroups.com.
Visit this group at http://groups.google.com/group/objectivej.
For more options, visit https://groups.google.com/d/optout.

Aparajita Fishman

unread,
Nov 4, 2015, 3:40:26 PM11/4/15
to objec...@googlegroups.com
> The [object setValue:newValue forKey:@"someProperty”] does indeed work well for the object’s properties.

That's because internally it notifies the bindings to update. If you change the property directly on a raw Javascript object, the Objective-J engine has no idea it has happened.

Regards,

Aparajita


Arjen Schipmolder

unread,
Nov 4, 2015, 4:06:44 PM11/4/15
to objec...@googlegroups.com
I had assumed the accessor setters would do a similar thing, but as that didn’t work this makes complete sense.
My classes and subclasses are just rather complex so that didn’t make troubleshooting any easier, but it’s starting to come together now.

By the way, your Vimeo video about Cup and how the bindings work was a huge help getting my app to work with bindings so thanks a lot for that one as well.

Arjen

Aparajita

unread,
Nov 4, 2015, 4:35:36 PM11/4/15
to Cappuccino & Objective-J
Generated accessors don't update bindings because they change the underlying instance variable directly, whereas bindings are actually implemented as a category on CPObject (CPKeyValueCoding). Since bindings are outside of the object hierarchy, it requires a call to setValue:forKey:.

If you are using your own Product class, you could define your own setters like this:

// Assuming instance variable is `price`
- (void)setPrice:(double)newValue
{
    if (price === newValue)
        return;

   
[self willChangeValueForKey:@"price"];
    price
= newValue;
   
[self didChangeValueForKey:@"price"];
}

Then you can use setters as usual and bindings will be updated.


On Wednesday, November 4, 2015 at 4:06:44 PM UTC-5, Arjen Schipmolder wrote:
I had assumed the accessor setters would do a similar thing, but as that didn’t work this makes complete sense.
My classes and subclasses are just rather complex so that didn’t make troubleshooting any easier, but it’s starting to come together now.

By the way, your Vimeo video about Cup and how the bindings work was a huge help getting my app to work with bindings so thanks a lot for that one as well.

Arjen



Reply all
Reply to author
Forward
0 new messages