id
Uniquely identify an instance or raw instance data.
connection.id(instance)
Returns the instance id as determined by algebra's id values if they exist, else return
the instance value indicated by idProp if it exists, otherwise the return the value of
instance.id
.
Parameters
- instance
{Instance|Object}
:An instance or raw properties for an instance.
Returns
{String|Number}
:
A string or number uniquely representing instance
.
Use
Many behaviors, such as the constructor/store, need to have a unique identifier for an
instance or instance data. This connection.id
method should return that.
Typically, an item's id is a simply property value on the object. For example, "Todo" data might look like:
{_id: 5, name: "do the dishes"}
In this case, algebra's id
property should be set to "_id":
var algebra = new set.Algebra({
set.props.id("_id")
});
connect([...],{algebra: algebra});
However, some data records may have compound ids. For example, "Class Assignment" data may be uniquely
identified by a combination of two properties, the studentId
and the classId
. For this kind of setup, you
can provide your own id function as follows:
var customIdBehavior = {
id: function(assignment){
return assignment.studentId + "-" + assignment.classId;
}
}
var classAssignmentConnection = connect(
[...behaviors..., customIdBehavior],
{
url: "/class_assignments"
}
);