mendix.lib.MxObject
Methods
mendix.lib.MxObject
is an Object that encapsulates a specific, unique instance of an Entity in the Domain Model of the current session. It provides methods for accessing and modifying attributes.
Instances of mendix.lib.MxObject
should never be instantiated manually. They are the result of calls into mx.data
such as mx.data.get or mx.data.create .
addReference(attr, guid)
Add an object reference to a reference attribute.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The name of the reference attribute. Can refer to either a reference or reference set attribute. |
guid | String | The guid of the object to add to the reference. |
Returns
Type | Description |
---|---|
Boolean | true if successful, false otherwise |
addReferences(attr, guids)
Add object references to a reference set attribute.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The name of the reference set attribute. |
guid | String | The guid of the object to add to the reference. |
Returns
Type | Description |
---|---|
Boolean | true if successful, false otherwise |
removeReferences(attr, guids)
Remove references from reference attribute attr
.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The refererence attribute to remove references from. |
guids | Array | The guids of the objects to remove from the reference. |
Returns
Type | Description |
---|---|
Boolean | false if an exception occured while removing the reference, true otherwise. |
compare(mxobj)
This method is used to compare this MxObject to another MxObject and find out if they are identical.
Parameters
Name | Type | Description |
---|---|---|
mxobj | MxObject | The MxObject to compare to. |
Returns
Type | Description |
---|---|
Boolean | true if they are identical, otherwise false. |
get(attr)
This method returns the typed value of an attribute.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The attribute whose value to return. |
Returns
Type | Description |
---|---|
Any | Value of the specified attribute. |
Examples
mxobj.get("IsActive"); // true
mxobj.get("Name"); // "John Doe"
mxobj.get("LoginCount"); // 315
fetch(path, callback)
Get an object or value of an attribute, through a path from this object. The result is passed to a callback and depends on the requested path:
Path resolves to | Result |
---|---|
attribute | The value of the attribute |
object reference | GUID of the object |
object reference set | Array of GUIDs of the objects |
entity | The object or an Array of objects (in case of a reference set) |
If no path is given, the result is set to the object itself. A reference set can only be the last reference in the path.
Parameters
Name | Type | Description |
---|---|---|
path | String | Path from the object to the desired object or attribute. |
callback | Function | Callback taking a single parameter, value , which is set to the requested object or attribute value. If there is an error evaluating the path, an Error exception is thrown. |
Examples
Retrieving an attribute value from an object is essentially a callback version of get()
:
mxobj.fetch("Name", function(value) {
alert("Person's name is " + value);
});
Retrieving an object through a reference:
mxobj.fetch("MyFirstModule.Friend/MyFirstModule.Person", function(value) {
alert("Name of person's friend is " + value.get("Name"));
});
When retrieving an object through a reference set, the result will be an Array:
mxobj.fetch("MyFirstModule.Owns/MyFirstModule.Pet", function(value) {
alert("Person owns the following pets: " + value);
});
Retrieving an attribute through several references:
mxobj.fetch("MyFirstModule.Friend/MyFirstModule.Person/" +
"MyFirstModule.Father/MyFirstModule.Person/" +
"Age", function(value) {
alert("Name of the father of this person's friend is " + value);
});
set(attr, val)
This method is used to set the value of attribute attr to the value of val. The type of val depends on the type of attribute.
This only changes the MxObject instance in the client session. To send the changes to the server or commit the object to the backend database, see mx.data.save and mx.data.commit , respectively.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The attribute to set. |
val | Any | The value to set. |
Returns
Type | Description |
---|---|
Boolean | true if change was successful, otherwise false |
Example
mxobj.set("Name", "John Smith");
mxobj.set("IsActive", true);
getEntity()
This method will return a String with this object’s Entity name.
Returns
Type | Description |
---|---|
String | String value of the Entity name. |
Example
mxobject.getEntity(); // "System.User"
getChild(attr)
XPath queries can be performed with a depth level. If this level is greater than 0 the references that each MxObject contains are filled with the MxObjects of the references (up to the depth level).
This in that case, instead of calling getReference to get the reference GUID, this method will return the MxObject.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The attribute to retrieve the reference Object from |
Returns
Type | Description |
---|---|
MxObject | MxObject for the reference. |
getChildren()
XPath queries can be performed with a depth level. If this level is greater than 0 the references that each MxObject contains are filled with the MxObjects of the references (up to the depth level).
This in that case, instead of calling getReferences to get the reference GUID, this method will return the MxObjects in an Array.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The attribute to retrieve the references Object from |
Returns
Type | Description |
---|---|
Array | Array of MxObjects |
getGuid()
This method will return the GUID of this object.
Returns
Type | Description |
---|---|
String | The GUID of the object |
Example
mxobject.getGUID(); // "1234567890131"
getReference()
Retrieve the object referenced by a reference attribute. When trying to retrieve a non-reference or reference set attribute this way, an exception is thrown.
Parameters
Name | Type | Description |
---|---|---|
attr | String | Reference attribute whose referenced object to return. |
Returns
Type | Description |
---|---|
MxObject | The object referenced by the given reference attribute. |
getReferences()
Retrieve the objects referenced by a reference or reference set attribute.
Parameters
Name | Type | Description |
---|---|---|
attr | String | Reference attribute whose referenced objects to return. |
Returns
Type | Description |
---|---|
Array | The referenced objects |
hasChanges()
This method is used to find out if a MxObject has changes.
Returns
Type | Description |
---|---|
Boolean | true if this MxObject has changes, otherwise false |
hasChildren(attr)
Check whether the reference attribute attr
contains any references.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The reference attribute to check |
Returns
Type | Description |
---|---|
Boolean | true if attr references any objects, false otherwise. |
isReadonlyAttr()
Check whether attribute attr
is read-only.
Parameters
Name | Type | Description |
---|---|---|
attr | String | The attribute for which to check whether it is read-only. |
Returns
Type | Description |
---|---|
Boolean | true if attr is a read-only attribute, false otherwise. |
getAttributes()
Convenience function, see MxMetaObject.getAttributes
.
Example
var attrs = mxobject.getAttributes(); // ["Name", "Age"];
getAttributesWithoutReferences()
Convenience function, see MxMetaObject.getAttributesWithoutReferences
.
Example
var attrs = mxobject.getAttributesWithoutReferences(); // [ "Name", "Age" ]
getReferenceAttributes()
Convenience function, see MxMetaObject.getReferenceAttributes
.
Example
var refs = mxobject.getReferenceAttributes(); // [ "Mod.Person_Parent",
// "Mod.Person_Company" ]
getEnumCaption(attr, key)
Convenience function, see MxMetaObject.getEnumCaption
.
Example
mxobject.getEnumCaption("Color", "red"); // "Rouge"
getSelectorEntity(attr)
Convenience function, see MxMetaObject.getSelectorEntity
.
Example
mxobject.getSelectorEntity("Order_OrderLine"); // "CRM.OrderLine"
getSubEntities()
Convenience function, see MxMetaObject.getSubEntities
.
Example
mxobject.getSubEntities(); // [ "MyModule.EntityA", "MyModule.EntityB" ]
getSuperEntities()
Convenience function, see MxMetaObject.getSuperEntities
.
Example
mxobject.getSuperEntities(); // [ "MyModule.EntityC",
// "MyModule.EntityD",
// "System.User" ]
hasSubEntities()
Convenience function, see MxMetaObject.hasSubEntities
.
Example
mxobject.getSubEntities(); // [ "MyModule.EntityA", "MyModule.EntityB" ]
hasSuperEntities()
Convenience function, see MxMetaObject.hasSuperEntities
.
Example
if (mxobject.hasSuperEntities()) {
alert("This object inherits from another Entity");
} else {
alert("This object does not inherit from another Entity");
}
isA(entity)
Convenience function, see MxMetaObject.isA
.
if (mxobject.isA("System.User")) {
alert("This object is a System.User");
} else {
alert("This object is not a System.User");
}
isBoolean(attr)
Convenience function, see MxMetaObject.isBoolean
.
Example
if (mxobject.isBoolean("Checked")) {
alert("Attribute 'Checked' is a Boolean");
} else {
alert("Attribute 'Checked' is not a Boolean");
}
isDate(attr)
Convenience function, see MxMetaObject.isDate
.
Example
if (mxobject.isDate("DoB")) {
alert("Attribute 'DoB' is a Date");
} else {
alert("Attribute 'DoB' is not a Date");
}
isEnum(attr)
Convenience function, see MxMetaObject.isEnum
.
Example
if (mxobject.isEnum("Colors")) {
alert("Attribute 'Colors' is an Enumeration");
} else {
alert("Attribute 'Colors' is not an Enumeration");
}
isLocalizedDate(attr)
Convenience function, see MxMetaObject.isLocalizedDate
.
Example
if (mxobject.isLocalizedDate("DoB")) {
alert("Attribute 'DoB' is a Localized Date");
} else {
alert("Attribute 'DoB' is not a Localized Date");
}
isNumber(attr)
Convenience function, see MxMetaObject.isNumber
.
Example
if (mxobject.isNumber("Count")) {
alert("Attribute 'Count' is a Number");
} else {
alert("Attribute 'Count' is not a Number");
}
isObjectReference
Convenience function, see MxMetaObject.isObjectReference
.
Example
if (mxobject.isObjectReference("Mother")) {
alert("Attribute Parent is a reference");
} else {
alert("Attribute Parent is not a reference");
}
isObjectReferenceSet
Convenience function, see MxMetaObject.isObjectReferenceSet
.
Example
if (mxobject.isObjectReferenceSet("Children")) {
alert("Attribute Children is a reference set");
} else {
alert("Attribute Children is not a reference set");
}
isPassword(attr)
Convenience function, see MxMetaObject.isPassword
.
Example
if (mxobject.isPassword("Password")) {
alert("Attribute 'Password' is a Password");
} else {
alert("Attribute 'Password' is not a Password");
}
inheritsFrom(entity)
Convenience function, see MxMetaObject.inheritsFrom
.
Example
if (mxobject.inheritsFrom("System.User")) {
alert("This object inherits from System.User");
} else {
alert("This object does not inherit from System.User");
}
isReference(attr)
Convenience function, see MxMetaObject.isReference
.
if (mxobject.isReference("Children")) {
alert("Attribute Children is a reference set");
} else {
alert("Attribute Children is not a reference set");
}
getEnumMap(attr)
Convenience function, see MxMetaObject.getEnumMap
.
Example
mxobject.getEnumMap("Color"); // [ { key : "red", caption : "Red" },
// { key : "green", caption : "Green" },
// { key : "blue", caption : "Blue" } ]
getOptions(attr)
Convenience function, see MxMetaObject.getOptions
.
Example
mxobject.getOptions("Color"); // [ "red", "green", "blue" ]
toString()
This method returns the GUID.
Returns
Type | Description |
---|---|
String | GUID of this MxObject |