Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

What is [new]?

Working my way through a block of code that contains some elements I'm not familiar with. This will actually be a string of several questions so feel free to weigh in however and whenever you like.

Here's the code my button is triggering:

    $('#btn-add-user').on('click', function () {
        $(window).trigger('user-form-show', ['new']);
    });

I traced that to this (I'm only including a portion of the code):

$(document).ready(function(){

    $("#chk-user-active").on('change', function () {
        $('#user-active').val(this.checked);        
    });
  var $uModal = $('#modal-user-form'),
      $form = $('#user-form'),
      $passwordButton = $form.find('.btn-change-password').clone(),
      $passwordForm = $form.find('.password-template').clone().removeClass('d-none'),
      data = [],
      loadedUserId = '',
      passwordRegex = {
        upper: /[A-Z]{1,}/,
        lower: /[a-z]{1,}/,
        number: /[0-9]{1,}/,
        special: /[\!\@\#\$\%\^\&\*]{1,}/,
        total: /.{8,}/
      };

  $form.find('.field-phone').mask('000-000-0000');

  $form.find('.password-template').remove();

  $(window).on('user-form-show', function(e, userId, opts){
    // prep the modal
    $uModal.find('.loading').show();
    $uModal.find('.main').hide();

    // show the modal loading screen
    $.magnificPopup.open({
      alignTop: true,
      items: {
        src: $uModal, // can be a HTML string, jQuery object, or CSS selector
        type: 'inline'
      }
    });

    // clear the cached data
    data = {};

    // clear the form from any previous data
    clearForm();
           

    // load the data if a valid id is provided
    if (typeof userId != 'undefined' && userId != 'new') {
      loadFormData(userId);
    } else {
      // set the id for a new user
        loadedUserId = 'new';
        // load drag & drop members 
        teamDragula(userId);

        $form.find('#user-role').attr('data-user-id', loadedUserId);
      // force the password fields
      $form.find('.btn-change-password').replaceWith( $passwordForm.clone() );
      $form.find('.btn-cancel-password').closest('.col').remove();

      // show the form
      $uModal.find('.loading').hide();
      $uModal.find('.main').show();
    }
  });

Open in new window


My first question pertains to the syntax used to trigger the "user-form-show" function:  $(window).trigger('user-form-show', ['new']);

I looked up "window," so I understand that, but what is [new] and why is it in brackets?

That's Question #1!
Avatar of skullnobrains
skullnobrains

The square brackets is the syntax for a list ( an unindexed array ) of values.

in this case, a list containing a single value.

Multiple ones would be comma separated.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Chris!

This is great!

One question: What is the "," character in the .trigger( event [, extraParameters ] ) syntax?

It's the first character after the first bracket. What does that stand for, what does it represent?
Hey Bruce,

That's just a comma. When you see documentation like that, anything in square brackets is considered 'optional', so this:

.functionName( param1 [, param2] );

just means that whatever is in the square brackets is optional. You could call it with either:

.functionName('someValue')
or
.functionName('someValue', 'anotherValue')
But technically, in the context of what we're working with here, in your example...

.functionName('someValue', 'anotherValue')

..."anotherValue" would be an array, yes?
In the context of the .trigger function - yes:

Basically, this:

.trigger( event [, extraParameters ] )

says the trigger method must take 1 parameter, but can also include a second, optional parameter.

This bit:

extraParameters
Type: Array or PlainObject
Additional parameters to pass along to the event handler.

Then says that if you do provide the second, optional parameter, then it needs to be either an Array or a Plain Object.
The reason I first showed it with 'someValue', 'anotherValue' was in an attempt to point out that the square brackets in the documentation doesn't mean an Array - it just means optional. It just so happens that in the trigger method, that optional parameter should also be an array (or object) - different uses of the square brackets!
Excellent!

BTW: Just so you know: When I'm asking questions pertaining to the kind of detail I'm trying to mine, this is all part of the way in which I tackle new projects / languages. You can see a sample of this at http://brucegust.com/adm/node/.

I try not to get into the weeds too much, but I value being able to do more than just copy and paste stuff when there's a need to truly understand the nomenclature of what you're looking at.

You would be gratified to know how much of your content is being reproduced in the context of the notes I'm taking like what you see in the "Node" course above!

I'm going to wrap this question up, but I've got another one at https://www.experts-exchange.com/questions/29172937/Do-I-have-to-write-e-every-time.html#questionAdd

Thanks!
Hey Bruce,

That's a great approach. Whenever I answer questions here on EE, I always try and give some context and explanation as to the why things are the way they are. It would be all to easy to post up some code and say this is what you need, but without the 'why' or the lack of any background, it fairly meaningless. Like you, I'd rather understand a solution than just paste it in.