0

So I'm trying to learn RequireJS for work. It seemed easy enough, but then I ran into a problem. How do I pass on an object from within one of the dependencies?

The code below returns "TypeError: Undefined is not a function" for the new variable. I tried logging the typeof GoogleMap, but it also returns undefined.

What is the correct way of doing this?

Main.js

require(['test', 'geo', 'googlemaps'], function (GoogleMap) {
        var map = new GoogleMap();
});

googlemaps.js

define(function() {

        function GoogleMap(){

           var i = 0;
           var userMarkers = [];

           var map = {}

           this.initialize = function(lat,lng){
                map = getMap(lat,lng);
           }

           var getMap = function(lat,lng){
               var asldOptions = {
                     zoom: 4,
                     center: new google.maps.LatLng(59, 18),
                     mapTypeId: google.maps.MapTypeId.ROADMAP
                 };

               map = new google.maps.Map(document.getElementById("map-canvas"), asldOptions);

               return map;
           }
        }
    }
);

Edit: I should add that it works fine when i remove the 'define' line at the top and the corresponding brackets at the bottom of googlemaps.js.

2
  • 2
    the function you pass to define in googlemaps.js needs to return something. You probably just want to return the GoogleMap function (which, incidentally, does not need to be named). Commented Mar 19, 2013 at 13:09
  • @DavidMcMullin, Could you give me an example of how such a return statement would look? return GoogleMap; isn't working, but it feels like i'm missing something. Commented Mar 19, 2013 at 13:16

2 Answers 2

1
define(function() {

    var GoogleMap  = function(){

       var i = 0;
       var userMarkers = [];

       var map = {}

       this.initialize = function(lat,lng){
            map = getMap(lat,lng);
       }

       var getMap = function(lat,lng){
           var asldOptions = {
                 zoom: 4,
                 center: new google.maps.LatLng(59, 18),
                 mapTypeId: google.maps.MapTypeId.ROADMAP
             };

           map = new google.maps.Map(document.getElementById("map-canvas"), asldOptions);

           return map;
       }
    }

    return GoogleMap;

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

2 Comments

That's a no-go, compadre. Sorry.
require(['test', 'geo', 'googlemaps'], function (GoogleMap) ===> GoogleMap is mapped to test not to googlemaps. That is your problem. Change it to require(['test', 'geo', 'googlemaps'], function (test, geo, GoogleMap) and try again
0
require(['test', 'geo', 'googlemaps'], function (GoogleMap) {

change that to

require(['test', 'geo', 'googlemaps'], function (test, geo, GoogleMap) {

and try again.

Your GoogleMap is not mapped to googlemaps script, but to test. it is in the order which you specify your dependencies.

Either way, my previous answer still stands. That is how you should be returning the function in your googlemaps.js file.

Hope that helps. Suj

Comments

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.