I am trying to build a lightning component that will be used on our Community site for article voting.
I have a custom object called Public_Article_Ratings__c with some simple fields I want to capture.
Here is the component markup:
<aura:component controller="ArticleVotingController" implements="forceCommunity:availableForAllPageTypes" >
<aura:attribute name="newVote" type="Public_Article_Rating__c"
default="{'sobjectType':'Public_Article_Rating__c',
'Article_ID__c':'',
'Article_URL__c':'',
'upVote__c':'',
'downVote__c':'',
'Comments__c':''
}"/>
<form>
<div id="ratingArea">
<span>Was this article helpful?</span>
<ui:inputCheckbox aura:id="voteUp" label="Yes"
value="{!v.newVote.upVote__c}"/>
<ui:inputCheckbox aura:id="voteDown" label="No"
value="{!v.newvote.downVote__c}" />
<ui:inputTextArea aura:id="articleComments" label="Comments"
value="{!v.newVote.Comments__c"/>
<br></br>
<ui:button label="Submit" labelClass="label"
press="{!c.createVote}"/>
</div>
</form>
</aura:component>
The client side controller:
({
createVote : function(component, event, helper) {
var newVote = component.get("v.newVote");
helper.createVote(component, newVote);
}
})
The helper:
({
createVote : function(component, vote) {
this.upsertVote(component, vote, function(a){
var votes = component.get("v.votes");
votes.push(a.getReturnValue());
component.set("v.votes", votes);
});
},
upsertVote : function(component, vote, callback){
var action = component.get("c.saveVote");
action.setParams({
"vote": vote
});
if (callback) {
action.setCallback(this, callback);
}
$A.enqueueAction(action);
}
})
And the server-side controller:
public with sharing class ArticleVotingController {
@AuraEnabled
public static Public_Article_Rating__c saveVote(Public_Article_Rating__c vote){
upsert vote;
return vote;
}
}
For now, I am just trying to capture the most simple fields to make sure it works (votes & comments). But I am getting an error in the console:
Looks like there's a problem: Unknown Error : Cannot read property 'push' of undefined
I was wondering if anyone with more Lightning Component building experience than me is able to pick out any flaws with the way I am trying to handle this. Thanks so much for reading.