0

I'm struggling to understand how to implement gotoDate properly. The documentation specifies a string based command structure (like so: .fullCalendar( 'gotoDate', date )), but does not specify an appropriate approach when the constructor contains variable flags. This is my current approach:

$('#eventCalendar').fullCalendar({
    height: "parent"
});

Simply adding .gotoDate(date) to the end of this structure does not yield any result at all. Do I need to remove the height flag in order to achieve this?

Notes:
* date is a moment as specified
* Height is specified by an external div element
* The date is supplied by a C# backend in the format yyyy-MM-dd

1 Answers1

1
$('#eventCalendar').fullCalendar({
    height: "parent"
});

(or similar, with whatever options you decide to use) initialises the calendar.

Then, after that you can call methods on it, e.g.

$('#eventCalendar').fullCalendar("gotoDate", date);

Calling a method requires a separate command, you can't do it in the same command as where you set the initial options.

N.B. It's not entirely clear if this is your intention, but if you just want to set the date which the calendar first starts, you can set the "defaultDate" option during initialisation:

$('#eventCalendar').fullCalendar({
    height: "parent",
    defaultDate: date
});
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • So initialisation and action have to be separate in JS? I had assumed that having a second `$('#eventCalendar').fullCalendar` would give me either a second calendar or would overwrite the first. –  Feb 11 '18 at 21:29
  • It's not a JavaScript-wide convention or anything, it's just how fullCalendar is written. I haven't studied the source code, but from the behaviour I observe it simply seems to work like this: The ".fullCalendar()" method accepts 1 or more arguments depending on the usage. The first argument is crucial - if you provide an object (e.g. `{ height: "parent" }`) it takes this as a list of options and initialises (or re-initialises) the calendar, attaching it to the element supplied in the jQuery selector ("#eventCalendar" in your case). – ADyson Feb 12 '18 at 08:20
  • However if the first argument is a string (e.g. "gotoDate") it assumes you want to run a command against the already existing calendar, and tries to find an internal command which matches what you wrote, and then execute it. Of course if you provide a method name, but against a selector where no calendar exists (e.g. `$("#someRandomID").fullCalendar("gotoDate", "2018-01-01");`, then nothing will happen, because there's no calendar to work on. – ADyson Feb 12 '18 at 08:23
  • I hope that makes some sense. P.S. If the answer helped you, please remember to mark it as "accepted" - thanks :-) – ADyson Feb 12 '18 at 08:23
  • That's... incredibly confusing, but it seems to work. Thanks! –  Feb 12 '18 at 09:47
  • No problem. What part do you find confusing? The internal software of fullCalendar can determine the type of the parameter you pass into it (object or string), and based on that it can decide what to do. Note that "gotoDate" is not a real JavaScript method (or at least, it's not defined as public so that other code can call it). The only actual JS method you can use is ".fullCalendar". The "methods" that fullCalendar's docs talk about such as "gotoDate" are just string variables it uses to decide what action to take (could be as simple as decided by a "switch" statement) – ADyson Feb 12 '18 at 09:50
  • Which, to me, seems a rather roundabout approach. Defining public methods seems like the more appropriate route here, I can't see why anyone would choose to use string testing, but I'm no JS dev so I could be missing something. –  Feb 12 '18 at 09:58
  • I'm no expert in plugin development but it seems to be a common pattern for this kind of plugin. It avoids conflicts I guess, if two plugins loaded had a "show" method callable directly on an element, it could get awkward. Bootstrap, big major plugin, uses this same pattern to show and hide modals, for instance. I guess there is a reason. Probably there's a blog or two about it somewhere. – ADyson Feb 12 '18 at 12:46