JS范围问题:如何重用此脚本中的位置数据

JS scope issue: How can I reuse the locations data in this script?

本文关键字:脚本 位置 置数据 何重用 范围 问题 JS      更新时间:2023-09-26

我有一个很长的脚本,它现在正在使用第二个AJAX调用来获取数据作为临时解决方案。

在进行建议调用/admin/locations/suggest.json的情况下,结果是一个对象数组。将显示一个列表,其中包含来自这些对象的数据。当点击其中一个时,会对/admin/locations/view/' + locationId + '.json进行第二次AJAX调用,以再次获取位置数据。这些数据已经存在,但在第一次AJAX调用返回的数据中。

我现在的问题是从on.click()代码内部访问Locations变量。我已经在那里得到了索引,除了locations之外,其他都没有数据。

如何在第一次调用后填充locations并在on.click()事件中使用它们?

SuggestLocation = function() {
    var locations = null;
    $('#NewLocation').hide();
    function suggestLocation(locations) {
        $.ajax({
            url: '/admin/locations/suggest.json',
            type: 'POST',
            data: $("#AgendaItemAdminAddForm, #AgendaItemAdminEditForm").serialize(),
            success: function (data) {
                var htmlString = '';
                for (var p in data.data) {
                    htmlString = htmlString + '<li data-index="' + p + '" data-id="' + data.data[p].Location.id + '">' + data.data[p].Location.display_name + '</li>';
                }
                $('#LocationSuggestions').html(htmlString);
                locations = data.data;
                console.log(locations);
            },
            dataType: 'json'
        });
    };
    $(document).on('click', '#LocationSuggestions li', function(event) {
        locationIndex = ($(this).data('index'));
        locationId = $(this).data('id');
        $.ajax({
            url: '/admin/locations/view/' + locationId + '.json',
            type: 'GET',
            data: null,
            success: function(data) {
                $('#SelectedLocation').html(
                    Mustache.render($('#LocationTemplate').html(), data.data)
                );
            },
            dataType: 'json'
        });
        $('#AgendaItemLocationId').val(locationId);
        $('#AgendaItemLocationId').val(locationId);
        $('#LocationFormFields').hide();
        $('#LocationSuggestions').html('');
        $('#NewLocation').show();
    });
    $('#LocationFormFields input, #LocationFormFields textarea').keydown(function() {
        suggestLocation();
    });
    $('#LocationFormFields select').change(function () {
        suggestLocation();
    });
    $('#NewLocation').click(function(event) {
        event.preventDefault();
        $('#AgendaItemLocationId').val($(this).data(''));
        $('#LocationFormFields').show();
        $('#SelectedLocation').hide();
        suggestLocation();
        $(this).hide();
        return false;
    });
};
SuggestLocation();

您所在的位置:

var locations = null;

在外部范围中创建位置null的分配是多余的,它没有任何用处),但是当您这样做时:

function suggestLocation(locations) {

它创建了一个locations变量,该变量是suggestLocation的本地变量,所以稍后执行时:

  locations = data.data;

数据被分配给位置变量,而不是外部变量。对suggestLocation的调用都没有将参数传递给函数,因此只需将locations作为一个正式参数,即使其成为:

function suggestLocation() {

因此该值被分配给外部位置,该位置可用于SuggestLocation内的所有函数。

只需记住AJAX调用是异步的,所以在尝试访问它之前,请确保回调已经被调用并分配了值

此外,按照惯例,以大写字母开头的函数名是为构造函数保留的,因此SuggestLocation是不合适的。除了一个字母的大写外,拥有两个名称相同的函数也不是一个好主意。