Model Object
compareTo()
Signature
Section titled “Signature”compareTo() — returns boolean
Available in: model
Category: Miscellaneous Functions
Description
Section titled “Description”Pass in another model object to see if the two objects are the same.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
object | component | yes | — |
Examples
Section titled “Examples”// 1. Check if two model objects are the same instance
user1 = model("User").findByKey(1);
user2 = model("User").findByKey(1);
user3 = user1;
isSame = user1.compareTo(user2);
// isSame -> false (two separate fetches produce distinct instances)
isSame = user1.compareTo(user3);
// isSame -> true (user3 is the same object reference as user1)
// 2. Guard against processing the same object twice in a loop
users = model("User").findAll(returnAs="objects");
currentUser = model("User").findByKey(session.userId);
for (u in users) {
if (!u.compareTo(currentUser)) {
// process all users except the currently logged-in one
sendNotification(u);
}
}