4

In the Developer Console Preview the "e.force:showToast" event is available but when fired does not produce any output. (Putting the same components in an App Builder app does produce output.)

Is there a reliable way to detect this situation? I'm indirecting through a component that could present the error inline in the page if it knew that the toast was going to fail.

(This Lightning: How to detect Salesforce1 context? isn't a solution as I can get a reference to the event but firing the event results in no output.)

0
setErrorMessage : function(cmp, msg) {
        var toastEvent = $A.get("e.force:showToast");
         if(toastEvent == undefined){
            var msgDiv = cmp.find("errorMessageDiv");
            cmp.set("v.errorMessage", msg);
         }else{
            toastEvent.setParams({
                "title": "Error",
              "message": msg,
              "type": "error"
            }); 
            toastEvent.fire();
        }
    },

Alternatively, you can register your event in component markup and then catch it in cases where you use Lightning out in Visualforce, etc. Registering event will not return undefined from $A.get().

This is useful for system events like navigateToComponent

  <aura:registerEvent name="force:navigateToComponent" type="force:navigateToComponent" />

This will return event, it will fire and you can code your custom logic around, for example in your Visualforce page:

function navigateToVisualforce(e) {
      var params = e.getParams();
      if (params.componentDef === 'NameSpace:Foo') {
        window.parent.location.href = '/apex/baz'
      } 
    }
    $Lightning.use("NameSpace:LightningOutApp", function () {
      $Lightning.createComponent("NameSpace:MyComponent", {}, "lightning", function (cmp) {
        // Listen to redirect events.
        $A.eventService.addHandler(
          {"event": "force:navigateToComponent", "handler": navigateToVisualforce}
        );
      });
    });
  • Thanks for answering. The problem I'm trying to solve is how to automatically identify "cases where you use Lightning out in Visualforce" which appears to include "Preview" from the Developer Console where the toast event is not undefined. – Keith C Nov 25 '17 at 10:33
  • Oooh 😮 I see what you mean, sorry! – dzh Nov 25 '17 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.