1

I have this simple lightning component:

<aura:component controller="myEventController">
    <aura:attribute name="newEvent" type="Event"
        default="{ 'sobjectType': 'Event',
        'Subject': ''}"/>
     <form>
           <fieldset>
                <ui:inputText aura:id="evtSubj" label="Subject"
                        class="form-control"
                        value="{!v.newEvent.Subject}" required="true" />
                    <ui:button label="Submit" press="{!c.debugEventMethod}"/>
                </fieldset>
       </form>
</aura:component>

and its controller:

({
    debugEventMethod: function(component, event, helper) {
            var evt= component.get("v.newEvent");
            alert(JSON.stringify(evt));
    }
})

The values are not getting set when sobject is Event. If i try with any other object (Say Opportunity and Name field), that works. Am I doing anything wrong here or is it a limitation or bug in lightning component? Any help will be appreciated.

| improve this question | | | | |
2

I have also had trouble with setting the default value of attributes to be an object. I feel that this is likely a bug since it works for some attribute types but not others. One workaround that you could consider is to set the value with the controller on component initialization.

<aura:component controller="LightningController">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="newEvent" type="Event" />
     <form>
           <fieldset>
                <ui:inputText aura:id="evtSubj" label="Subject"
                        class="form-control"
                        value="{!v.newEvent.Subject}" required="true" />
                    <ui:button label="Submit" press="{!c.debugEventMethod}"/>
                </fieldset>
       </form>
</aura:component>


({
    debugEventMethod: function(component, event, helper) {
            var evt= component.get("v.newEvent");
            alert(JSON.stringify(evt));
    },
    doInit : function(component, event, helper) {
        var evtInitialValue = { 'sobjectType': 'Event', 'Subject': ''};
        component.set("v.newEvent", evtInitialValue);
    }
})

By the way, if you look at the type of the event, you will see that when you set the default value, you are getting a string instead of an object, and that is what is preventing you from setting property values.

debugEventMethod: function(component, event, helper) {
        var evt= component.get("v.newEvent");
        alert(typeof evt);
}
| improve this answer | | | | |
  • Hi Martin, its not the default value I am much concerned about, but rather the fact that any field of this particular object is not getting set in the form. So any value I enter in the UI, is not getting set in the Event object attribute . The workaround I had to do is create a new attribute corresponding to each field and then pass them to Apex class and set Event object field values to those. – Mahesh Aug 25 '15 at 13:40
  • @Mahesh The problems are related. For some reason, when setting the default value for some attribute types as an object, the component leaves it as a string rather than converting it to an object. – martin Aug 25 '15 at 13:45

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.