Class: mendix/lib/MxObject

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

Constructor

new mendix/lib/MxObject()

Constructs a new MxObject.

Methods

addReference(attr, guid) → {boolean}

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

Parameters:
Name Type Description
attr string

The reference attribute. Can refer to either a reference or reference set attribute.

guid GUID

GUID of the object to add to the reference

Returns:

true if successful, false otherwise

Type
boolean
Example
mxobj.addReference("MyFirstModule.Ent_RefEnt", "12345");

addReferences(attr, guids) → {boolean}

Add an object to a reference set attribute.

Parameters:
Name Type Description
attr string

the reference set attribute

guids Array.<GUID>

GUIDs of the objects to add to the reference

Returns:

true if successful, false otherwise

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

compare(mxobj) → {boolean}

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 mendix/lib/MxObject

MxObject to compare with

Returns:

mxobj true if they are identical, false otherwise

Type
boolean
Example
mxobj.compare(mxobj); // true

fetch(path, callback)

Gets 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 value of the attribute
object reference GUID of the object
object reference set array of GUIDs with the objects
entity array of GUIDs for a reference set, MxObject otherwise

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 mendix/lib/MxObject~FetchCallback

called when fetching is done

Examples

Retrieving an attribute value from an object is essentially a callback version of mx.data.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);
});

get(attr) → {string|number|external:Big|boolean}

Returns the value of an attribute.

For reference attributes, use mendix/lib/MxObject#getReference and mendix/lib/MxObject#getReferences instead.

Parameters:
Name Type Description
attr string

attribute whose value to return

Returns:

value of the specified attribute. The return type depends on the type of the attribute as follows:

Attribute type Javascript type Javascript value
Integer, Long, Float, Currency string string representation of the number
Autonumber string string representation of the number
Decimal external:Big instance containing the number
DateTime number timestamp of the date
Enum string value of the enumeration
String string
Boolean boolean
Type
string | number | external:Big | boolean
Examples
mxobj.get("IsActive");   // true
mxobj.get("Name");       // "John Doe"
mxobj.get("LoginCount"); // "315"

getAttributes()

Convenience function

See:
Example
var attrs = mxobject.getAttributes(); // ["Name", "Age"];

getEntity() → {string}

Gets the entity name.

Returns:

the entity name

Type
string
Example
mxobject.getEntity(); // "System.User"

getEnumCaption()

Convenience function

See:
Example
mxobject.getEnumCaption("Color", "red"); // "Rouge"

getEnumMap()

Convenience function

See:
Example
mxobject.getEnumMap("Color"); // [ { key : "red",   caption : "Red" },
                              //   { key : "green", caption : "Green" },
                              //   { key : "blue",  caption : "Blue" } ]

getGuid()

Returns the GUID of this MxObject.

Example
mxobj.getGuid(); // "1234567890131"

getOptions()

Convenience function

See:
Example
mxobject.getOptions("Color"); // [ "red", "green", "blue" ]

getReference(reference) → {string}

Retrieves 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
reference string

attribute whose referenced object to return

Returns:

GUID of the MxObject referenced by the given reference attribute

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

getReferenceAttributes()

Convenience function

See:
Example
var refs = mxobject.getReferenceAttributes(); // [ "Mod.Person_Parent",
                                              //   "Mod.Person_Company" ]

getReferences(attr) → {Array.<GUID>}

Retrieves the MxObjects referenced by a reference or reference set attribute.

Parameters:
Name Type Description
attr string

reference attribute whose referenced objects to return

Returns:

GUIDs of the references objects

Type
Array.<GUID>
Example
// Get GUIDs of objects over association MyFirstModule.Ref.
refs = mxobj.getReferences("MyFirstModule.Ref");    // [ "12345", "12346" ]

getSelectorEntity()

Convenience function

See:
Example
mxobject.getSelectorEntity("Order_OrderLine"); // "CRM.OrderLine"

getSubEntities()

Convenience function

See:
Example
mxobject.getSubEntities(); // [ "MyModule.EntityA", "MyModule.EntityB" ]

getSuperEntities()

Convenience function

See:
Example
mxobject.getSuperEntities(); // [ "MyModule.EntityC",
                             //   "MyModule.EntityD",
                             //   "System.User" ]

hasChanges() → {boolean}

Checks an MxObject for changes.

Returns:

true if this MxObject has changes, false otherwise

Type
boolean
Example
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());
    }
});

hasSubEntities()

Convenience function

See:
Example
mxobject.getSubEntities(); // [ "MyModule.EntityA", "MyModule.EntityB" ]

hasSuperEntities()

Convenience function

See:
Example
if (mxobject.hasSuperEntities()) {
    alert("This object inherits from another Entity");
} else {
    alert("This object does not inherit from another Entity");
}

inheritsFrom()

Convenience function

See:
Example
if (mxobject.inheritsFrom("System.User")) {
    alert("This object inherits from System.User");
} else {
    alert("This object does not inherit from System.User");
}

isA()

Convenience function

See:
Example
if (mxobject.isA("System.User")) {
    alert("This object is a System.User");
} else {
    alert("This object is not a System.User");
}

isBoolean()

Convenience function

See:
Example
if (mxobject.isBoolean("Checked")) {
    alert("Attribute 'Checked' is a Boolean");
} else {
    alert("Attribute 'Checked' is not a Boolean");
}

isDate()

Convenience function

See:
Example
if (mxobject.isDate("DoB")) {
    alert("Attribute 'DoB' is a Date");
} else {
    alert("Attribute 'DoB' is not a Date");
}

isEnum()

Convenience function

See:
Example
if (mxobject.isEnum("Colors")) {
    alert("Attribute 'Colors' is an Enumeration");
} else {
    alert("Attribute 'Colors' is not an Enumeration");
}

isLocalizedDate()

Convenience function

See:
Example
if (mxobject.isLocalizedDate("DoB")) {
    alert("Attribute 'DoB' is a Localized Date");
} else {
    alert("Attribute 'DoB' is not a Localized Date");
}

isNumber()

Convenience function

Deprecated:
See:
Example
if (mxobject.isNumber("Count")) {
    alert("Attribute 'Count' is a Number");
} else {
    alert("Attribute 'Count' is not a Number");
}

isNumeric()

Convenience function

See:
Example
if (mxobject.isNumeric("Count")) {
    alert("Attribute 'Count' is numeric");
} else {
    alert("Attribute 'Count' is not numeric");
}

isObjectReference()

Convenience function

See:
Example
if (mxobject.isObjectReference("Mother")) {
    alert("Attribute Parent is a reference");
} else {
    alert("Attribute Parent is not a reference");
}

isObjectReferenceSet()

Convenience function

See:
Example
if (mxobject.isObjectReferenceSet("Children")) {
    alert("Attribute Children is a reference set");
} else {
    alert("Attribute Children is not a reference set");
}

isPassword()

Convenience function

See:
Example
if (mxobject.isPassword("Password")) {
    alert("Attribute 'Password' is a Password");
} else {
    alert("Attribute 'Password' is not a Password");
}

isReadonlyAttr(attr) → {boolean}

Checks whether an attribute is read-only.

Parameters:
Name Type Description
attr string

attribute for which to check

Returns:

true if attr is a read-only attribute, false otherwise

Type
boolean

isReference()

Convenience function

See:
Example
if (mxobject.isReference("Children")) {
    alert("Attribute Children is a reference set");
} else {
    alert("Attribute Children is not a reference set");
}

removeReferences(attr, guids) → {boolean}

Removes references from a reference attribute.

Parameters:
Name Type Description
attr string

refererence attribute to remove references from

guids Array.<GUID>

GUIDs of the objects to remove from the reference

Returns:

false if an exception occured while removing the reference, true otherwise

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

set(attr, val) → {boolean}

Sets the value of attribute attr to the value of val.

The type of val depends on the type of attribute.

This only changes the mendix/lib/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

attribute to set

val *

value to set

Throws:

Error if value is invalid, use validator#validate before calling this method

Returns:

true if change was successful, false otherwise

Type
boolean
Examples
mxobj.set("Name", "John Smith");
mxobj.set("IsActive", true);

Type Definitions

FetchCallback(requested)

Callback for returning data with mendix/lib/MxObject#fetch.

Parameters:
Name Type Description
requested *

object, attribute value or an Error object when there was an error.