Get access to the facility from another js function



  • I'll rewrite it. Here's the code:

        token: '111',
    
    run() {
        this.addressSuggestionsInit();
    },
    completeFields($this, suggestionsData) {
        let address = suggestionsData.data;
        let container = $this.closest('[data-suggestions-container]');
        let completeInputs = container.find('[data-equal-field-from]');
    
        completeInputs.each(function() {
            let input = $(this);
            let name = input.attr('data-equal-field-from');
            const value = address[name];
            const type = address[name + TYPE];
            let fullValue = '';
    
            if ('street' === name || 'city' === name) {
                fullValue = type && value ? `${type}. ${value}` : '';
            }
    
            if ('region' === name) {
                const regionTypeFull = address.region_type_full;
                fullValue = regionTypeFull && value ? `${value} ${regionTypeFull}` : '';
            }
    
            if (value) {
                input.val(fullValue ? fullValue : value);
            } else {
                input.val('');
            }
            input.change();
        });
    
        $this.change();
    },
    
    addressSuggestionsInit(container = false) {
        let self = this;
        let $address = container ? $(`${container} [data-suggestions-address]`) : $('[data-suggestions-address]');
    
        $address.each(function() {
            let $this = $(this);
            $this.suggestions({
                count: 10,
                type: 'ADDRESS',
                minChars: 3,
                token: self.token,
                onSelect(suggestionsData) {
                    console.log('suggestionsData');
                    console.log(suggestionsData);
                    self.completeFields($this, suggestionsData);
                },
                addon: 'none',
            });
        });
    },
    

    };

    I need access to suggestionsData.

    What I did:

    1. I put collback in the initialization of this violin so I can get the object out.
    suggest.run(function(suggestionsData) {
    console.log('вызов из коллбэка' + suggestionsData.data);
    });
    1. The violin itself changed as follows:
    run(suggestion) {
    this.addressSuggestionsInit();
    this.suggestion = suggestion; //добавил коллбэк
    },
    1. Put the collback in the place where the work is proceeding with suggestionData:
    onSelect(suggestionsData) {
    console.log('suggestionsData');
    console.log(suggestionsData);
    this.suggestion(); // по моему замыслу это должен быть вызов коллбэка и передача его с параметром наверх, но тут исполнение скрипта прерывается ошибкой с сообщением о том, что this.suggestion is not a function
    self.completeFields($this, suggestionsData);
    },

    I'd appreciate it if they told me what I'm doing wrong and how to access the properties of the facility outside.



  • I don't understand what you've got in the example, but on your comments, I understand the following:

    //Есть некая функция, которая в процессе работы отдает объект (suggestionsData) в консоль.
    

    let suggestionsData = { name : 'I am object'}

    function printInConsole(suggestionsData, callback){
    console.log(suggestionsData.name)
    callback(suggestionsData)
    }

    //Как получить к этому объекту доступ из другой функции? Ваш колбек

    function sayHi(suggestionsData){
    //тут я получаю доступ к объекту 'suggestionsData'
    console.log('Hi there!' , suggestionsData.name)
    }

    printInConsole(suggestionsData, sayHi)

    If I've got it wrong, please change the question.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2