简体   繁体   中英

Calling function in coffee script returns "function is not defined"

I'm trying to call a function "remove_category" from the code below, but it returns 'Uncaught ReferenceError: remove_category is not defined'

$('.category-checkbox').on 'click', ->
    category_id = $(this).attr('id')
    $(this).toggleClass 'selected'
    if $(this).hasClass('selected')
        save_category category_id
    else
        remove_category category_id
    return

remove_category

window.remove_category = (remove_category) ->
        alert 'called'
        sessionStorage.removeItem 'categories', remove_category
        stored_categories = jQuery.grep(stored_categories, (value) ->
            value != remove_category
        )
        console.log stored_categories
        sessionStorage.setItem 'categories', JSON.stringify(stored_categories)
        return

I'm not sure how to fix this problem. Can anyone please help me?

Thank you!

I solved the issue. It was simply, my indentation was not right. The corrected code is here.

$('.category-checkbox').on 'click', ->
    category_id = $(this).attr('id')
    $(this).toggleClass 'selected'
    if $(this).hasClass('selected')
        save_category category_id
    else
        remove_category category_id
    return

remove_category = (remove_category) ->
    sessionStorage.removeItem 'categories', remove_category
    stored_categories = jQuery.grep(stored_categories, (value) ->
        value != remove_category
    )
    console.log stored_categories
    sessionStorage.setItem 'categories', JSON.stringify(stored_categories)
    return

I forgot to put the head of remove_category to same position as $('.category-checkbo').on.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM