1

Been stuck on this for a while, cant figure out why my upsert method is not being called! Im using lightning, Can you help?

The line where i get the error is :

this.createAppointment(component, updateAppointment);

I have used a function further up the page, which works no problem.

defaultDate: this.processDate(), //'2016-01-01',

Helper Code

({
loadCal : function(component, appointmentsList){

    var arrayLength = appointmentsList.length;
    //alert(appointmentsList);
    var eventsList = [];
    for (var i = 0; i < arrayLength; i++) {
        {
            eventsList.push({
                Id: appointmentsList[i].Id,
                title: appointmentsList[i].Name,
                start: appointmentsList[i].risetest__Date__c + 'T' + appointmentsList[i].risetest__Start_Time__c + ':00',
                end: appointmentsList[i].risetest__Date__c + 'T' + appointmentsList[i].risetest__End_Time__c + ':00'
            });
        }
    }

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek ,agendaDay'
        },
        defaultDate: this.processDate(), //'2016-01-01',
        editable: true,
        eventDrop: function(event, delta, revertFunc) {
            //alert(event.Id + " was dropped on " + event.start.format());
            var updateAppointment ;

            if (confirm("Are you sure about this change?")) {
                try {
                    console.log(event);
                    var tempNewDate = event.start._d;
                    console.log(  tempNewDate.getFullYear() + '-' + ('0' + (tempNewDate.getMonth()+1)).slice(-2) + '-' + ('0' + tempNewDate.getDate()).slice(-2));

                    var tempObject = {
                        Id: event.Id,
                        risetest__Date__c : tempNewDate
                    }
                    console.log(tempObject);

                    component.set("v.appointment", tempObject);

                    updateAppointment = component.get("v.appointment");
                    console.log(updateAppointment);

                    this.createAppointment(component, updateAppointment);

                }catch(err) {
                    console.log(err);
                }
            }

        },
        eventClick: function(calEvent, jsEvent, view) {

            alert('Event: ' + calEvent.Id);

        },
        eventLimit: true, // allow "more" link when too many events
        events: eventsList
    });
},

createAppointment: function(component, appointment) {
this.upsertAppointment(component, appointment, function(a) {
    var Appointments = component.get("v.Appointment");
  });
},

upsertAppointment : function(component, a, callback) {
    var action = component.get("c.saveAppointment");
    action.setParams({ 
        "appobj": a
    });
    if (callback) {
      action.setCallback(this, callback);
    }
    $A.enqueueAction(action);
},

processDate : function(dt){
    var MyDate = new Date();
    var MyDateString;
    MyDateString = MyDate.getFullYear() + '-' + ('0' + (MyDate.getMonth()+1)).slice(-2) + '-' + ('0' + MyDate.getDate()).slice(-2);
    return MyDateString;
}
})
  • I am no expert but may be its because the function createAppointment is called one function further inside than from where the processDate was called? – Santanu Halder Feb 19 '16 at 12:16
  • Thats what I was thinking @SantanuHalder, but the other function seems to work. although that is not an event. I'll try messing around with it, move it outside. – c14kaa Feb 19 '16 at 12:36
3

You need to use function.bind() to generate a method with a captured this value (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).

You also need to use $A.getCallback() to capture the current access level (see https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_lightning_get_callback.htm). Without this your subsequent use of cmp.get() and cmp.set() will fail.

Try with this code

`$('#calendar').fullCalendar({
  ...
  eventDrop: $A.getCallback(function(event, delta, revertFunc) {
      ...
  }.bind(this)), 
  ...
});`
  • I just want to say I love you. Thank you so much for this. I am reading up on this and it looks i may need to use this more for future dev with the calendar. – c14kaa Feb 22 '16 at 9:49
1

I think what is happening is an issue of scope for the This keyword. I think because you are assigning the function to the eventDrop property, this is referring to that object. Your previous use with the defaultDate did not use "this" inside another function. Try instead of using the this keyword, using

MyComponentHelper.createAppointment(component, updateAppointment);

See "As an object method": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

  • I did try Helper.createAppointment(component, updateAppointment); But I believe this issue is even deeper! Right, i think this part of the problem. but its also the fact that the the code is added to the component view page. So calling the helper method from the view wont work. I need to call a method that is accessible on the view and can push the data into the controller or helper. – c14kaa Feb 19 '16 at 13:58
  • You need to call createAppointment from the component view? Can't you just call a method on your controller from your view to call the helper method? – trankz Feb 19 '16 at 14:21

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.