1

What is the way to create instance of Apex type inside aura:attribute or js controller (inside js onInit <aura:handler name="init" value="{!this}" action="{!c.onInit}" />) ?

For SObject it looks like:

<aura:attribute name="newContact" type="Contact"
default="{ 'sobjectType': 'Contact',
           'LastName': 'Smith',
           'AccountId': '001i000002zxpz1' 
         }"/>

What is the right way to do it through component or js?

3

You can use the model="" named attribute on your lightning component, or app. See below:

First line of my lightning application:

<aura:application model="ConfigurationModel" controller="ConfigurationController">

Here's my ConfigurationModel Apex class:

/**
* @author Charles Naccio
* @date 10/14/2015
*
* @group Configuration
* @group-content ../../ApexDocContent/Configuration.htm
*
* @description Configuration model class which is used by the Configuration lightning application. This class 
* provides the data needed for the lightning application's initial state.
*/
public with sharing class ConfigurationModel extends lightning_Response implements lightning_IResponse {

    /*******************************************************************************************************
    * @description List of supported gateways that can be configured.
    */
    @AuraEnabled
    public List<String> supportedGateways {get;set;}

    /*******************************************************************************************************
    * @description Primary configured gateway. Although configuration is setup to support multiple gateways
    * we currently only support a single configured gateway.
    */
    @AuraEnabled
    public Gateway gateway {get;set;}

    /*******************************************************************************************************
    * @description Nested class to model gateway configuration in a lightning friendly way
    */
    private class Gateway {

        /*******************************************************************************************************
        * @description Gateway name
        */
        private String name {get;set;}

        /*******************************************************************************************************
        * @description Gateway login. This can either be an actual login to be used with password below, 
        * or an API key; whichever is needed for authentication.
        */
        private String login {get;set;}

        /*******************************************************************************************************
        * @description Gateway password if applicable
        */
        private String password {get;set;}

        /*******************************************************************************************************
        * @description Gateway application id; not used by all gateways
        */
        private String applicationId {get;set;}

        /*******************************************************************************************************
        * @description If gateway is in test mode; defaults to false.
        */
        private Boolean testMode {get;set;}

        /*******************************************************************************************************
        * @description Configuration tab index controls where this configuration element is displayed in the UI
        */
        private Double tabIndex {get;set;}

        /*******************************************************************************************************
        * @description Create new instance of gateway configuration
        * @example
        * ConfigurationModel.Gateway gateway = new ConfigurationModel.Gateway(); 
        */
        private Gateway(Configuration configuration) {
            configuration_Merchant_Gateway gatewayConfiguration = configuration.merchant.gateways.primary;
            this.name = gatewayConfiguration.name;
            this.login = gatewayConfiguration.login;
            this.password = gatewayConfiguration.password;
            this.applicationId = gatewayConfiguration.applicationId;
            this.testMode = gatewayConfiguration.testMode;
            this.tabIndex = gatewayConfiguration.tabIndex;
        }

        /*******************************************************************************************************
        * @description Get configured gateway name
        * @return Configured gateway name
        * @example
        * String gatewayName = gateway.getName(); 
        */
        @AuraEnabled
        public String getName() {
            return this.name;       
        }

        /*******************************************************************************************************
        * @description Get configured gateway Login
        * @return Configured gateway login
        * @example
        * String gatewayLogin = gateway.getLogin(); 
        */
        @AuraEnabled
        public String getLogin() {
            return this.login;      
        }

        /*******************************************************************************************************
        * @description Get configured gateway password
        * @return Configured gateway password
        * @example
        * String gatewayPassword = gateway.getPassword(); 
        */
        @AuraEnabled
        public String getPassword() {
            return this.password;       
        }

        /*******************************************************************************************************
        * @description Get configured gateway applicationId
        * @return Configured gateway applicationId
        * @example
        * String gatewayApplicationId = gateway.getApplicationId(); 
        */
        @AuraEnabled
        public String getApplicationId() {
            return this.applicationId;      
        }

        /*******************************************************************************************************
        * @description Get configured gateway testMode
        * @return Configured gateway testMode
        * @example
        * Boolean gatewayTestMode = gateway.getTestMode(); 
        */
        @AuraEnabled
        public Boolean getTestMode() {
            return this.testMode;       
        }

        /*******************************************************************************************************
        * @description Get configured gateway tabIndex
        * @return Configured gateway tabIndex
        * @example
        * Double gatewayTabIndex = gateway.getTabIndex(); 
        */
        @AuraEnabled
        public Double getTabIndex() {
            return this.tabIndex;       
        }
    }

    /*******************************************************************************************************
    * @description Create new instance of Configuration Model
    * @example
    * ConfigurationModel configurationModel = new ConfigurationModel(); 
    */
    public ConfigurationModel() {
        try {
            Configuration configuration = new Configuration();
            this.supportedGateways = configuration.merchant.gateways.supported;
            this.gateway = new Gateway(configuration);
        } catch(Exception e) {

            // Setup error
            this.error = new lightning_Response_Error(e);
        }
    }

    /*******************************************************************************************************
    * @description Return lightning response error
    * @example
    * lightning_Response_Error = new ConfigurationModel.getError();
    */
    @AuraEnabled
    public lightning_Response_Error getError() {
        return this.error;
    }
}
2

Natallia, I actually asked the same question back in Winter '15, and I'm not sure whether the process is any different now in Spring '15. You can take a look at the answer to my question for specific instructions on how to initialize an attribute using an Apex method.

I'm definitely interested in learning from any others who have more experience or a better method of initializing attributes for Lightning components, because I don't like the fact that the component needs to make a round-trip call to Salesforce just to initialize some variables.

  • @ Marty C., Totally agree with your. Thanks a lot for the response. – Natallia Mar 9 '15 at 21:44

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.