adjust fullcalendar data source when resizing

Is there a way to update the eventSource on eventResize when the event in the callback is the new event, making it difficult to find it in the eventSource?

function eventResized ( event, delta, revertFunc, jsEvent, ui, view ) {

    var end = moment(event.end).add(delta._milliseconds, 'milliseconds');
    var index = $scope.availableEvents.events.indexOf(event);
    $scope.availableEvents.events[index].end = end;

  }

In the provided code snippet, index is set to -1 which means that event cannot be found in $scope.availableEvents.events. How can this issue be resolved?

Answer №1

Dealing with a similar issue led me to discover the following solution.

function adjustEvent ( event, delta, revertFunc, jsEvent, ui, view ) {

    var end = moment(event.end).add(delta._milliseconds, 'milliseconds');
    var matchingEvents = $scope.calendar.fullCalendar('clientEvents', function (existingEvent) {
                        return (existingEvent._id == event._id);
    });
    console.log(matchingEvents);
  }

This method proved effective for my scenario.

Answer №2

Utilize the $$hashKey.

function adjustEventSize ( event, delta, revertFunc, jsEvent, ui, view ) {

    var end = moment(event.end).add(delta._milliseconds, 'milliseconds');

    $scope.availableEvents.events.map(function(eventModel) {

        // locate event by hashKey
        if (eventModel.$$hashKey === event.$$hashKey) {
             eventModel.end = end;
        }
    });
}

I opted to use a comprehensive update function where I search by $$hasKey and sanitize the model manually to maintain the cleanliness of the events collection. Here's a more detailed example (disregard the excessive use of $scope, as it is just for representation):

$scope.adjustEventSize = function(event, delta, revertFunc, jsEvent, ui, view) {
    $scope.updateEvent(event, delta, revertFunc, jsEvent, ui, view);
};

$scope.addNewEvent = function (momentDate) {
    // default to adding event today
    momentDate = momentDate || moment();
    $scope.events.push($scope.cleanseEventData({
        title: 'New Event',
        startTimestamp: momentDate.unix(),
        start: momentDate.format(),
        allDay: true,
        stick: true // prevents new events from disappearing when switching views
    }));
};

// an encapsulation to correctly update angular models from fullcalendar events
// also tidies up dates
$scope.updateEvent = function(event, delta, revertFunc, jsEvent, ui, view) {
    $scope.events.map(function(eventModel) {
        // locate event by hashKey
        if (eventModel.$$hashKey === event.$$hashKey) {
            eventModel.allDay = event.allDay;
            eventModel.start = event.start;
            eventModel.end = event.end;
            $scope.cleanseEventData(eventModel);
        }
    });
};

$scope.cleanseEventData = function(eventModel) {

    eventModel.start = moment.utc(eventModel.start).format();
    eventModel.startTimestamp = moment(eventModel.start).unix();

    if (eventModel.end) {
        eventModel.end = moment.utc(eventModel.end).format();
    }

    // other sanitization steps ...

    return eventModel;
};

$scope.eventSources = retrieveEventSources();

function retrieveEventSources() {

    // fetch pre-processed json script template
    $scope.jsonEvents = jsonCache.get($attrs.jsonDataId);
    $scope.events = $scope.jsonEvents.events;
    $scope.events.map($scope.cleanseEventData);
    return [$scope.events];
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating through multiple sections by scrolling to the h2 titles

I am in the process of creating an online book that will consist of multiple chapters. In each chapter, I want to include a sidebar with scroll-navigation that automatically links to every h2 heading within that specific chapter. I have experimented with ...

Remove any elements using jQuery that do not contain any content

I need to remove any empty elements from my HTML code. Here is an example of the HTML markup: The HTML markup is stored in a jQuery variable called 'myhtml' and it may contain various tags. <h1> <u> <strong></st ...

Why doesn't the address bar automatically update to the correct path after successfully authenticating with NextAuth using credentials?

Is there a way to automatically refresh the URL path once a successful login is completed with credentials? I attempted to set up credential authentication similar to the guide provided by Next in their tutorial here. However, I am only using email for au ...

Invoke JavaScript when the close button 'X' on the JQuery popup is clicked

I am implementing a Jquery pop up in my code: <script type="text/javascript"> function showAccessDialog() { var modal_dialog = $("#modal_dialog"); modal_dialog.dialog ( { title: "Access Lev ...

Does element.click() in Protractor's Webdriver method return a promise, or is there a way for it to handle errors?

Is the element(by.css()).click() method returning a promise, or is there a way to catch and assert against any errors that may occur? In my scenario, I have a component that is not clickable, and I want to handle the error when this happens. I also want t ...

How can you access additional fields beyond what is displayed in a dropdown select menu in React?

I am working with an array of Jsons that contain the fields ID, name, and description. My goal is to create a dropdown selection box that will show both the name and description when clicked, and then store the associated ID in the rawID state. I have been ...

What are the reasons for a jQuery function to run in a selective manner?

There seems to be some inconsistency in the behavior of this incomplete script that I'm trying to debug. The issue arises when I click off an item, as sometimes the $(editObj).removeAttr('style'); line of code executes and other times it doe ...

The Google Maps Javascript API will only load data if the console is open

I'm facing an issue with displaying a map on my webpage where I want to set a marker. What's the problem Despite no errors in the console or from Google, the map data is not loading properly. All I see is the Google logo and a grey background, ...

Establish the API URL according to the configuration settings in the Web.config file

In the Angular application I inherited, the API URL is defined as a constant variable. While it works, it's tedious to change the variable value every time I need to switch between deployment, testing, and development APIs. Is there a way for me to a ...

Is it possible to retrieve the ng-app value during a Protractor test?

Recently, I made some changes to the code I am testing. It now looks like this: <html ng-app="home" ng-strict-di=""><head>.... While running my protractor tests, I realized that I need to access the value of ng-app in order to compare and ide ...

Summon the keyboard to appear

How do I make the keyboard appear on my website? I have a javascript function that recognizes keyboard input, but I am struggling to display the keyboard on my mobile device. Is there a way to simulate traditional input and generate key events? I should ...

Tips for inserting items into an array of objects?

I have an array of objects with categories and corresponding points, and I need to calculate the total points for each category. { category: A, points:2 }, { category: A points: 3 }, { category: B, points: ...

Cease the continuous playback on the chromeless player of YouTube

When I play videos on my website, I use the function provided below. I want the videos to start automatically, but I don't want them to repeat once they end. I tried changing the loop parameter to loop=0, but unfortunately, it didn't work. Do I n ...

Utilizing Angular JS to execute a callback function upon receiving data from a Web Service

Attempting to incorporate a controller callback function within my service, triggering upon the success of an $http post request. The code snippet below provides a more detailed description. Controller : function UserAccountCtrl (UserService, $rootScope, ...

Inadequate data being sent to the server from Angular2 post request

Currently, I have a form field whose value I am passing to a service as this.form.value. However, when I log this.form.value on the console, I see Object { email: "zxzx", password: "zxzxx" }. Despite this, when I send the same data to the service and make ...

Is it possible to invoke $httpbackend with varying urls?

When using a service that calls a REST URL to retrieve data, I encountered an issue when trying to test it in Karma. Initially, I defined $httpBackend with the expected URL for each test. However, it was suggested that this approach was not ideal. Here is ...

What is the best way to implement a Fibonacci sequence using a for...of loop?

**Can someone show me how to generate Fibonacci numbers using the for...of loop in JavaScript?** I've tested out the following code and it's giving me the desired output: function createFibonacci(number) { var i; var fib = []; // Initi ...

Divider displayed between images in Internet Explorer 8

On my website, I have arranged four images in a square using the code below: <div id="tempo_main"> <div id="tempo_content"> <div style="text-align: center;z-index: 3;position: absolute;right:350px; left:350px; t ...

Use jQuery to update the location.href

I am facing a challenge in changing the location.href of certain URLs on my website. These URLs are embedded in product cards and do not include the letter "a", which complicates the process. Below is the HTML code snippet in question: <div class=" ...

Ensure that the search input field is in focus whenever the showSearch state is true

Having difficulty focusing on the input field whenever the showSearch state is true. Utilizing useRef and useEffect for this purpose. When the showSearch flag changes, the useEffect hook is triggered to check if showSearch is true, and if so, it should foc ...