Adding messages to the MessageBar control dynamically
The MessageBarcontrol displays messages to the user. You can add messages to a SPEAK page when designing the page in Sitecore Rocks. However, in practice, you often do not know at design-time if you need to show messages or the text of any possible messages.
In this situation, you can add the messages to the SPEAK page dynamically. You still add a MessageBar control to the page, but you do not add any messages to the control.
For example, if the MessageBar control is called MessageBar1 in your page, you can add messages with JavaScript like this:
define(["sitecore"], function (Sitecore) {
var Messages = Sitecore.Definitions.App.extend({
initialized: function () {
this.MessageBar1.addMessage("notification", {
text: "Read this",
actions: [{ text: 'Resolve', action: 'javascript: alert("Alert")' }],
closable: true
});
}
});
return Messages;
});
This just adds the message in PageCode, and as-is this code would just add a message once SPEAK initialized the page.
The addMessage method adds messages. The first parameter is the type of message. You can specify the following literal values:
-
error -
warning -
notification
The second parameter is an object:
-
text: this is the actual text the MessageBar control displays -
actions: this creates action links in the message.textis the text displayed for the link;actionis the action that visitors trigger when they click the link. -
closable: if true, visitors can dismiss the message
You can insert multiple action links in a message by adding to the actions object.
The following method removes all message that the MessageBar control displays currently:
this.MessageBar1.removeMessages()