2

can anyone tell me why this is not working?

ui = (function() {
collabElement = document.getElementById( 'approveCollab' );
    if(collabElement)
        collabElement.onclick = function(){editor.collaborate(); removeOverlay();}

    deleteElement = document.getElementById( 'approveDelete' );
    if(deleteElement)
        deleteElement.onclick = function(){editor.deletePost(); removeOverlay();}
})();

"collaborate" is an exported function in "editor.js" file. removeOverlay()" is a function in the same file. when "collabElement" is clicked only "removeOverlay" is being called.

there are no errors, just that the function is not called at all.

these are the function being called from editor.js:

function collaborate( event ) {

    console.log("started");
    var url = '';

    var postID = document.querySelector('.save').getAttribute('id');
    var recipient = document.querySelector('.collab-input').value;

    //validate email syntax
    var atpos=recipient.indexOf("@");
    var dotpos=recipient.lastIndexOf(".");
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
        console.log("wrong email");
        document.querySelector('.email-error').style.display = "block";
    }
    else{
        console.log("sending email to " + recipient);
        document.querySelector('.email-error').style.display = "none";
        if(postID != "new"){
            url = url + "?id=" + postID + "&recipient=" + recipient;

            var request = new XMLHttpRequest();
            request.open("GET", "collaborate"+url, true);
            request.send();
        }
    }
}

function deletePost( event ) {

    var url = '';

    var postID = document.querySelector('.save').getAttribute('id');
    if(postID != "new"){
        url = url + "?id=" + postID;

        var request = new XMLHttpRequest();
        request.open("GET", "delete"+url, true);
        request.send();
    }
}

2 Answers 2

4

If you want to call a function add () to it.

editor.collaborate()

(instead of editor.collaborate, which will just only address the function)

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

5 Comments

thanks Peter. obviously, you're right :) though when adding (), I get an error Object #<object> has no method 'collaborate'. when doing the same for another function works just fine.
Then the object editor hasn't that method. Is the editor.js somewhere on a public url so I can look at it?
look at my edited question for reference. the deletePost() function works just fine. i am really not getting this...
Is that the full content of editor.js? Or are these functions within a someClassName = function() { function deletePost() { .. } this.deletePost = deletePost; }? Where does editor gets defined? When there is a function collaborate within the editor.js doesn't neccessarily mean that you can call it with editor.collaborate(). That function can be defined within a different scope, so you can't call it that way.
thanks Peter! I was missing the return in: return { init: init, saveState: saveState, publishState: publishState, collaborate: collaborate, deletePost: deletePost, getWordCount: getWordCount }
2

I suspect the problem is that your IIFE is not returning anything; ui will always be undefined. I think you want this:

ui = (function() {
    collabElement = document.getElementById( 'approveCollab' );
    if(collabElement)
        collabElement.onclick = function(){editor.collaborate; removeOverlay();}
    //return collabElement so it's assigned to ui
    return collabElement;
})();

EDIT

While it's true your IIFE does not return anything, it looks like Peter's answer is more relevent to you at the moment; collaborate is not being called. His appears to be the right answer to this question.

1 Comment

thanks Adam, I just copied some of the code for the function, i initialize it by returning init() which calls all relevant functions.

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.