Mendix Client 5 API Documentation


mendix.lib.MxObject

An 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 seldom be instantiated manually. They are the result of calls into mx.data such as mx.data.get or mx.data.create.

Methods

addReference(attr, guid)

If attr is a reference attribute, set it to the given object. If attr is a reference attribute, add the given object to it.

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.

Example

mxobj.addReference("MyFirstModule.Ent_RefEnt", "12345");

addReferences(attr, guids)

Add an object to a reference set attribute.

Parameters

Name Type Description
attr String The name of the reference set attribute.
guids Array The guids of the objects to add to the reference.

Returns

Type Description
Boolean true if successful, false otherwise

Example

mxobj.addReferences("MyFirstModule.Ent_RefEnt", ["12345", "12346"]);

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.

Example

mxobj.removeReferences("MyFirstMdule.Ent_RefEnt", ["12345", "12346"]);

compare(mxobj)

This method is used to compare this MxObject to another MxObject and find out if they are the same. This means they have the same Entity type and their attributes have the same value.

Parameters

Name Type Description
mxobj MxObject The MxObject to compare to.

Returns

Type Description
Boolean true if they are identical, otherwise false.

Example

mxobj.compare(mxobj); // true

get(attr)

This method returns the value of an attribute. For reference attributes, use getReference and getReferences.

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"

getGuid()

This method will return the GUID of this object.

Returns

Type Description
String The GUID of the object

Example

mxobj.getGuid(); // "1234567890131"

getReference()

Retrieve the GUID of 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
String The GUID of the object referenced by the given reference attribute.

Example

// Get GUID of object over association MyFirstModule.Ref.
refs = mxobj.getReference("MyFirstModule.Ref"); // "12345"

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 GUIDs of the referenced objects.

Example

// Get GUIDs of objects over association MyFirstModule.Ref.
refs = mxobj.getReferences("MyFirstModule.Ref");    // [ "12345", "12346" ]

hasChanges()

This method is used to find out if a MxObject has changes.

Returns

Type Description
Boolean true if this MxObject has changes, false otherwise.
mx.data.get({
    guid: "12345",
    callback: function(obj) {
        // Object is fresh from the runtime; next line prints 'false'.
        console.log(obj.hasChanges());

        obj.set("Name", "foo");

        // Object has a changed attribute; next line prints 'true'.
        console.log(obj.hasChanges());
    }
});

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" ]