canjs/can-set

Add id() method to algebra

Closed this issue · 2 comments

var Car = DefineList({
   vin: "number",
   brand: "string"
});
Car.algebra = new set.Algebra( set.props.id("vin") );

var myCar = new Car({vin: 1, brand: "Nissan"});

// for an instance:
Car.algebra.id( myCar );                       // >>>  1

// for raw data:
Car.algebra.id( {vin: 2, brand: "Mazda"} );    // >>>  2

This can be helpful when working with both instances and raw data, e.g. in can-connection's map-deep-merge we need to know how to get id from data.

One consideration will be something like can-connect/can/map's id method. If it's a can.Map, it needs to read the properties differently.

We might need to also have an idProps ... so can-map can be read via .attr(). Of course, if can-operate land, algebra.id() should be able to use can-operate to read properties.

can-connect/can/map's id also needs to ignore observations if the id is being read during eventSetup or teardown via callCanReadingOnIdRead. This part might be avoided by using Observation.ignore.

One consideration is that CanJS should use valueOf to specify some unique representation.

Alternatively, this sort of id could be something symbols can provide.