Here is my code. I followed the Lightning Components Developer Guide for the most part. I left out parts about updating the running count of records and changed how the 'newCase' attribute is created and populated the input data.
My guess is this is it where its going wrong. This is due to me setting the newCase values to the respective fields AFTER pulling the newCase attribute from the view. Either this or the fact that I am passing events in createCase and upsertCase in the helper class function parameters instead of cases. Trying to pass cases gives me an error that says the following:
Something has gone wrong. Error during init [TypeError: Cannot read property 'apply' of undefined].Please try again.
The component runs all the way to the very end, but a new Case is not created. When I console.log(evnent) at the very end, it shows the same Case that I input data for.
Here is the link for the developer guide.
Controller
public with sharing class MyCases {
@AuraEnabled
public static List<Case> getCases(){
Id myId = UserInfo.getUserId();
System.debug('myId+++++++++++'+myId);
List<Case> clist = [Select Id, Subject, Type, Origin, Status From Case];
System.debug('clist++++++++++'+cList.size());
return cList;
}
@AuraEnabled
public static Case saveCase(Case ca) {
// Perform isUpdateable() check here
upsert ca;
return ca;
}}
Component
<aura:component implements="forceCommunity:availableForAllPageTypes,flexipage:availableForAllPageTypes,force:appHostable" controller="MyCases">
<ltng:require styles="/resource/SLDS_Stylesheet/assets/styles/salesforce-lightning-design-system-ltng.css" />aura:attribute name="cases" type="Case[]"/>
<aura:attribute name="newCase" type="Case"
default="{ 'sobjectType': 'Case',
'Subject': '',
'Origin': '',
'Status': '',
'Contact': ''
}"/>
<aura:handler name="init" value="{!this}" action="{!c.init}"/>
<!-- Input Form using components -->
<div class="container">
<form class="slds-form--stacked">
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputText aura:id="subject" label="Subject"
class="slds-input"
labelClass="slds-form-element__label"
value=""
required="true"/>
</div>
</div>
<div class="slds-form-element slds-is-required">
<div class="slds-form-element__control">
<ui:inputText aura:id="origin" label="Origin"
class="slds-input"
labelClass="slds-form-element__label"
value=""
required="true"/>
</div>
</div>
<div class="slds-form-element">
<div class="slds-form-element__control">
<ui:inputText aura:id="status" label="Status"
class="slds-input"
labelClass="slds-form-element__label"
value=""
required="true"/>
</div>
</div>
<div class="slds-form-element">
<div class="slds-form-element__control">
<ui:inputText aura:id="contact" label="Contact Name"
class="slds-input"
labelClass="slds-form-element__label"
value=""
required="true"/>
</div>
</div>
<div class="slds-form-element">
<ui:button label="Submit"
class="slds-button slds-button--neutral"
labelClass="label"
press="{!c.createCase}"/>
</div>
</form>
</div>
</aura:component>
Controller Javascript Class
({
init : function(component, event, helper) {
helper.getCases(component);
},
createCase : function(component, event, helper) {
var subjectField = component.find("subject");
var subject = subjectField.get("v.value");
var originField = component.find("origin");
var origin = originField.get("v.value");
var statusField = component.find("status");
var status = statusField.get("v.value");
var contactField = component.find("contact");
var contact = contactField.get("v.value");
var newCase = component.get('v.newCase');
newCase.subject = subject;
newCase.origin = origin;
newCase.status = status;
newCase.contact = contact;
console.log(newCase);
helper.createCase(component, newCase);
console.log('End of createCase');
}})
Controller Javascript Helper Class
({
getCases: function(component) {
var action = component.get("c.getCases");
action.setCallback(this, function(response) {
var state = response.getState();
if (component.isValid() && state === "SUCCESS") {
component.set("v.cases", response.getReturnValue());
}
});
$A.enqueueAction(action); },
createCase: function(component, event) {
this.upsertCase(component, event, function(a) {
var cases = component.get("v.cases");
cases.push(a.getReturnValue());
component.set("v.cases", cases);
});
},
upsertCase : function(component, event, callback) {
var action = component.get("c.saveCase");
action.setParams({
"case": event
});
if (callback) {
action.setCallback(this, callback);
}
$A.enqueueAction(action);
},})