identity
Specifies that the property uniquely identifies instances of the type.
Boolean
If true
, specifies that the property uniquely identifies instances of the
type. identity
configures the result of getIdentity.
The following specifies that the id
property values uniquely identifies Todo
instances:
import { ObservableObject, Reflect as canReflect } from "can/everything";
class Todo extends ObservableObject {
static props = {
id: {
type: Number,
identity: true
},
name: String,
complete: Boolean
};
}
const todo = new Todo({id: 6, name: "mow lawn"});
console.log( canReflect.getIdentity(todo) ); //-> 6
identity
can be true
for multiple properties. If multiple identity properties
are specified, a sorted JSON string is returned:
import { ObservableObject, Reflect as canReflect } from "can/everything";
class Grade extends ObservableObject {
static props = {
classId: {
type: Number,
identity: true
},
studentId: {
type: Number,
identity: true
},
grade: String
};
}
const grade = new Grade({classId: 5, studentId: 7, grade: "A+"});
console.log( canReflect.getIdentity(grade) ); //-> "{'classId':5,'studentId':7}"
Use
identity
is useful for models like can-rest-model, can-connect and
can-query-logic. For example, it's used by isNew
to know if the instance has been saved to the server or not. It's used by real-time to know which instance to update when updateInstance is called.