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 {DefineMap, Reflect as canReflect} from "can";
const Todo = DefineMap.extend("Todo",{
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 {DefineMap, Reflect as canReflect} from "can";
const Grade = DefineMap.extend("Grade",{
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.