To get the ID of the currently viewed record, use implements="force:hasRecordId"
on your component. To get the user's Id, use {!$CurrentUser.Id}
. More generically, if you want to store stuff, sessionStorage
and localStorage
is the best you're going to get for storing global data without some extra effort. However, if you wanted to, you could make a global storage component.
This is the absolutely minimum, no frills, no security, no error handling, no singleton model, etc. Do not use this in production code as-is. It's simply not meant to handle all the possible scenarios that might come up. Do, however, use it to understand why creating this implementation is likely problematic in the first place; it would be notoriously difficult to get everything working just right.
c:globalDataSet.evt
<aura:event type="APPLICATION" description="Sets a global value for later retrival">
<aura:attribute name="data" type="Object" />
<aura:attribute name="name" type="String" />
</aura:event>
c:globalDataGet.evt
<aura:event type="APPLICATION" description="Gets a global value">
<aura:attribute name="callback" type="Function" />
<aura:attribute name="name" type="String" />
</aura:event>
c:globalStorage.cmp
<aura:component>
<!-- note: should ideally only be used once in the entire hierarchy -->
<aura:handler event="c:globalDataGet" action="{!c.getData}" />
<aura:handler event="c:globalDataSet" action="{!c.setData}" />
<aura:attribute name="data" type="Map" access="private" />
</aura:component>
c:globalStorageController.cmp
({
getData: function(component, event) {
var params = event.getParams(),
data = component.get("v.data");
params.callback(data.get(params.name));
},
setData: function(component, event) {
var params = event.getParams(),
data = component.get("v.data");
data[params.name] = data;
component.set("v.data", data);
}
})
Note: This is obviously only a very basic implementation. A full-featured implementation would be able to detect multiple instances and share the data efficiently across those instances. As written, you only need one instance of this component anywhere on the layout, and all components can dispatch events to request or store data.