S
S
Sergey2017-03-29 12:52:08
JavaScript
Sergey, 2017-03-29 12:52:08

How to pass parameters to js library?

Good day!
In the project I use Calendario.js to create an event calendar.

After loading the page and initializing the calendar, I send an ajax request, where I receive data about events in json format.

Upon successful receipt of data, I loop through all the elements, and try to create an event in the calendar for each.

$.ajax({
        type: "POST",
        url: '/ajax/getListEvent.php',
        dataType: 'json',
        data: ({
          action:'getlist',
          month: 3,
          year: 2017,
        }),
        success: function(data) {
          $.each(data, function(i, item) {
            var date  = item.date;
            var img = item.img;
            
            cal.setData({
              date : '<img src="'+img+'" />'
              //'03-31-2017' : '<img src="/upload/558/114_80_2/123.jpg" />'
            });
          });
        }
      });


The library error falls out in the console, we look at the line with checking values ​​for errors:

if(/^\d{2}-\d{2}-\d{4}/.test(key) || /^\d{2}-\d{2}-YYYY/.test(key) || /^\d{2}-DD-YYYY/.test(key) || /^MM-\d{2}-YYYY/.test(key) ||
        /^\d{2}-DD-YYYY/.test(key) || /^MM-\d{2}-\d{4}/.test(key) || /^\d{2}-DD-\d{4}/.test(key) || key == 'TODAY') {} else
         console.log(key + ' is an Invalid Date. Date should not contain spaces, should be separated by \'-\' and should be in the ' + 
          'format \'MM-DD-YYYY\'. That ain\'t that difficult!');


Date in the correct format. Therefore, I output the value to the console:

console.log(key, val);

Response in the console:

date <img src="/upload/558/114_80_2/123.jpg" />

That is, we understand that date was passed as a string, and img was interpreted normally.
And if, when adding an event, comment out the line with the passed parameters, and immediately pass the values

cal.setData({
              
              '03-31-2017' : '<img src="/upload/558/114_80_2/123.jpg" />'
            });


Then everything works fine ..
Tell me, please, what am I doing wrong when transferring data?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2017-03-29
@RnD

try

cal.setData({
    [date] : '<img src="'+img+'" />'
});

learn.javascript.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question