0

I have two JavaScript file which has some functions within a name space. But when i try to invoke this function from my main page it is throwing an error telling "Object # has no method 'LoadAllBooks'"... But I have invoked the the function by using namespace.function.

Here is the code in the JavaScript file Util.js

var myNameSpace = {
    Book: function (author, title, URL) {
        this.author = author;
        this.title = title;
        this.URL = URL;
    },
    LoadAllBooks: function (metadata, attachPoint) {
        Some Code--
    },
    arr: [],
    oneBook: {}
};

Now the above function LoadAllBooks is called from my html home page as shown below.

<script>
    var attachpoint = document.querySelector('.buttonAttachPoint');
    $(document).on('load',myNameSpace.LoadAllBooks("ajax/metadata.json",this.attachpoint));
</script>

Could some please tell me why this is giving out an error?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2208533
  • 37
  • 2
  • 5
  • Got the error "Object # has no method 'LoadAllBooks'" on the console. – user2208533 Mar 28 '13 at 02:23
  • The error suggests that myNameSpace is defined but it does not have the member `LoadAllBooks`. Is it possible that your other js file is evaluated after Util.js, and it overwrites the myNameSpace object? – tcovo Mar 28 '13 at 02:30

1 Answers1

0

The code inside the Util.js seems fine syntaxically. However, there are various issues with the calling code.

Assuming you are using jQuery, you should have something similar to:

//Specify some code to be ran when the document is ready using the $ function
//This could have been done like this also $(document).ready(function () {});
$(function () {
    //Notice that the following line is inide the document ready handler.
    //Always make sure that the code that manipulates DOM nodes is executed only
    //when the document is ready.
    var attachPoint = document.querySelector('.buttonAttachPoint');

    myNameSpace.LoadAllBooks('ajax/metadata.json', attachPoint);
});

plalx
  • 42,889
  • 6
  • 74
  • 90