1

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();
});
2
  • 2
    Where is the coffeescript source code? Commented Feb 17, 2014 at 14:51
  • 1
    obviously that's not the full output of the compiled coffeescript Commented Feb 17, 2014 at 14:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. window.imageUpdate is working fine. Thanks a lot for your great help.
Now that error is not coming. dblclick event not getting called. Kindly help me on this.While page loads getting alert "called" but when i double click an image the event doesn't getting trigger.
@user2138489 I don't see any obvious error with the dblclick event, it's impossible to say anything based on just this code alone. It's also a bit of a different question, you should start a new question & copy the coffeescript source and relevant HTML. Set up a test page if you can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.