user2138489
user2138489

Reputation: 75

ReferenceError: function name is not defined

I am getting ReferenceError: imageUpdate is not defined error
I provided my js code below:
I converted this code from coffeescript.

imageUpdate = function() {
  alert("called");
  return $(".image_content img").dblclick(function() {
    var current_img, img_align, img_caption, img_data, img_width;
    img_data = $(this).attr("src");
    img_width = $(this).parent(".image_content")[0].style.width;
    img_width = img_width.substring(0, img_width.length - 1);
    img_align = $(this).parent(".image_content").attr("data-align");
    img_caption = $(this).siblings(".caption").text();
    current_img = $(this);
    return $.ajax({
      url: "/books/" + $("#book_id").val() + "/images_list/",
      data: {
        type: "edit",
        image_width: img_width,
        image_caption: img_caption,
        image_align: img_align
      },
      success: function(data) {
        $("#oebImageListModal .modal-dialog").html(data);
        getUpdateSelection(current_img);
        return $("#oebImageListModal").modal("show");
      }
    });
  });
};


getUpdateSelection = function(current_img) {
  $("#cancel_insert").click(function() {
    $("#oebImageListModal").modal("hide");
    return false;
  });
  return $("#update_picture").click(function() {
    var div_style, img_align, img_caption, upd_width;
    img_caption = $("#image_caption").val();
    img_align = $("#image_align").val();
    upd_width = $("#image_width").val();
    upd_width += "%";
    if (img_align === "Middle") {
      div_style = "margin:0 auto;";
    } else if (img_align === "Left") {
      div_style = "float:left;";
    } else {
      div_style = "float:right;";
    }
    current_img.parent(".image_content").attr("style", div_style);
    current_img.parent(".image_content")[0].style.width = upd_width;
    current_img.parent(".image_content").attr("data-align", img_align);
    current_img.siblings(".caption").text(img_caption);
    return $("#oebImageListModal").modal("hide");
  });
};

return $(window).load(function() {
  loadOebPart();
  imageUpdate();
});

Upvotes: 1

Views: 677

Answers (1)

Martin Tournoij
Martin Tournoij

Reputation: 27822

I suspect these are actually 2 files, and imageUpdate is defined in one, and called in the other.

CoffeeScript compiles into an anonymous self-executing function, the code:

imageUpdate = -> alert '42'

Compiles to:

(function() {
    var imageUpdate;

    imageUpdate= function() {
        alert('42');
    };
})();

As you can see, imageUpdate is local to the anonymous function, and the global scope isn't touched. This is a real advantage, because you will never accidentally clobber the global window scope.

You probably want to explicitly assign this function to window, like so:

window.imageUpdate = -> alert '42'

Alternatively, you can use the -b switch of the coffee compiler to disable this top-level function wrapper. I would highly recommend against this though.

Upvotes: 1

Related Questions