4

I have a component that needs to behave differently during the design an runtime (it makes a web service call which might incur charges).

How do I figure out if I am in the design or run time? I don't see much in the definition:

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

controller:

({
    doInit: function(cmp) {
        cmp.set("v.viewUrl", "");
    }
})

Is there another a parameter I can use?

  • I hvn't thot abt this in detail.. but anything in the url params that can help u here ? – Vamsi Krishna Aug 18 '15 at 0:37
2

Unless we have surfaced something that will tell you when you are in design time versus runtime, I think your best bet is to use window.location.pathName to find whether your component is being rendered by app builder, or one of the salesforce application runtime contexts.

This is fundamentally a breakable way to approach this, and creative workaround (total hack). So I would go with detecting appBuilder as this is probably going to be the most stable thing to look for. Like this:

if (window.location.pathname==='/visualEditor/appBuilder.app') {
  ...do your design time logic here...
} 

I would suggest setting a boolean in your helper at init time of your component.

The way I've done this is as follows.

In my helper I'll place the attribute that defines design time:

{
  isDesignTime : false
}

In my controller's init:

{
  doInit: function(component, event, helper) {

    helper.isDesignTime = window.location.pathname==='/visualEditor/appBuilder.app'

  }
}

I'm just doing this without having tested it. This might work. If it doesn't, it is probably Aura trying to prevent arbitrary JS from being called. If that is the case, you can use $A.run() to invoke your JS code...but that may require a bit of acrobatics around closures, but is not impossible.

  • a couple of corrections (now that I am trying it out) #1 the window location is /flexipageEditor/surface.app - seems like this is probably to be just as reliable as the other location. Second I am actually putting the variable into the component front end because most of the changes are on the UI. <aura:attribute name="designTime" type="Boolean" default="true" access="private"/>. In any case, your idea was a solid one from the start. – mikebz Aug 18 '15 at 17:54
  • Glad it helped. I was just using the browser devtools and not testing directly from the component context. Must ask the product team about this when I get a chance. Will come back and clean up later, too. Time for bed. – pchittum Aug 19 '15 at 0:05
  • 1
    Come to think of it @mikebz, I'd like to hear more about your use case for this. I've thought of it, too, and it seemed like something component developers might eventually want. This probably deserves a user story for App Builder/Lightning Components. Some details from you might help me write it up. Drop me a line: pchittum-at-salesforce-dot-com – pchittum Aug 19 '15 at 0:14

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.