0
############################################CampingListForm.cmp
<aura:component >

    <aura:registerEvent name="addItem" type="c:addItemEvent"></aura:registerEvent>

    <aura:attribute name="newItem" type="Camping_Item__c" default="{'Name':'',
        'Quantity__c':0,
        'Price__c':0,
        'Packed__c':false,
        'sobjectType':'Camping_Item__c'}"/>

    <form class="slds-form--stacked">

        <lightning:input aura:id="campingform" label="Camping Name" name="expensename" value="{!v.newItem.Name}" required="true"/>
        <lightning:input type="number" aura:id="campingform" label="Quantity" name="expenseamount" min="1" value="{!v.newItem.Quantity__c}" messageWhenRangeUnderflow="Enter minimum 1 Quantity"/>
        <lightning:input aura:id="campingform" label="Price" formatter="currency" name="expenseclient" value="{!v.newItem.Price__c}"/>
        <lightning:input type="checkbox" aura:id="campingform" label="Packed" name="expreimbursed" checked="{!v.newItem.Packed__c}"/>

        <lightning:button label="Create Camping" class="slds-m-top--medium" variant="brand" onclick="{!c.clickCreateItem}"/> 

    </form>

</aura:component>
#########################################campingListFormController.js
({
    clickCreateItem : function(component, event, helper) {

         var validCamping = component.find('campingform').reduce(function (validSoFar, inputCmp) {

            inputCmp.showHelpMessageIfInvalid();

            return validSoFar && inputCmp.get('v.validity').valid;

        }, true);

        if(validCamping){

            var newCampingItem = component.get("v.newItem");

            var item = JSON.parse(JSON.stringify(newCampingItem));

            helper.createItem(component,item);

        //helper.createCamping(component,newCampingItem);
        /*var campings = component.get("v.items");


        campings.push(item);

        component.set("v.items",campings);
        component.set("v.newItem",{ 'sobjectType': 'Camping_Item__c','Name': '','Quantity__c': 0,
        'Price__c': 0,'Packed__c': false });*/

        }
    }
})
##############################################campingListFormHelper.js
({
    createItem : function(component,item) {

        var values=component.get("v.newItem");

        var event=component.getEvent("addItem");

        event.setParam({
            "item":values   
        });

        event.fire();

    }
})
##################################################addItemEvent.evt
<aura:event type="COMPONENT" description="Event template" >

    <aura:attribute name="item" type="Camping_Item__c"></aura:attribute>

</aura:event>
##################################################campingList.cmp
<aura:component controller="CampingListController">

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

        <aura:handler name="addItem" event="c:addItemEvent" action="{!c.handleAddItem}"></aura:handler>

        <aura:attribute name="items" type="Camping_Item__c[]"/>

        <c:campingListForm></c:campingListForm>

        <div class="slds-card slds-p-top--medium">
            <c:campingHeader/>
            <section class="slds-card__body">
                <div id="list" class="row">
                    <aura:iteration items="{!v.items}" var="item">
                        <c:campingListItem item="{!item}"/><br/>
                    </aura:iteration>
                </div>
            </section>
        </div>

</aura:component>
##########################################campingListController.js
({
    handleAddItem:function(component,event,helper){

        var result=event.getParam("item");

        helper.save(component,result);
    },


    doInit:function(component,event,helper){

        var action=component.get("c.getItems");

        action.setCallback(this,function(response){

            if(response.getState()=="SUCCESS"){
                component.set("v.items",response.getReturnValue());
            }
            else{
                console.log (response.getState());
            }
        })

        $A.enqueueAction(action);


    }
})
##################################campingListHelper.js
({
    save : function(component,item) {

        var obj=component.get("c.saveItem");

        obj.setParams({
            "camp":item
        });

        obj.setCallback(this,function(response){

            if(response.getState()=="SUCCESS"){

               var arrcomp=component.get("v.items");
               arrcomp.push(response.getReturnValue());
               component.set("v.items",arrcomp);
            }
        });

        $A.enqueueAction(obj);

    }
})

Event fires correctly with the values from the form I checked with console.log() In the CampingList.cmp -> controller in the handleAdditem function when I check with console.log(event.getParam("item") event parameters are not passed here I am getting this errror when I check the challenge "The campingList JavaScript controller isn't calling 'saveItem' in the Apex class to save the record or setting the 'item' as a parameter for the 'saveItem' action."

  • Apex method is also get call but with null values – Sameera Silva Dec 28 '18 at 7:01
  • "item":JSON.parse(JSON.stringify(component.get("v.newItem"))), – Sameera Silva Dec 28 '18 at 7:31

Your Answer

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

Browse other questions tagged or ask your own question.