从揭示模块模式返回值

时间:2015-04-02 15:39:37

标签: javascript function geolocation revealing-module-pattern

我现在正在学习揭示模块模式,到目前为止我做得还不错。但是我想知道当从另一个显示模块模式中调用一个显示模块模式函数时,你能否将一个值返回给调用者模式函数?

例如,我有一个包含多个函数的位置模块模式,我希望使用另一个模块模式。这是代码:

位置显示模块模式

// Revealing Modular pattern, containing functions relevant to location functionality    
var Location = (function(window, undefined){

    // Gets co-ordinates of users current position (TRUE: Run geoLocate function, FALSE: Run geoError function)
    function getCoords(){            
        navigator.geolocation.getCurrentPosition(getLocation, provideError);
    }

    // Get users current location
    function getLocation(position){

        var lat = position.coords.latitude;
        var lng = position.coords.longitude;

        // Convert seperate variables into one string type variable (Google geocode expects string format)
        var myLocation = String(lat+","+lng);

        console.log(myLocation);

    }

    // Error handler for geolocation
    function provideError(error){            
        console.log(error);
    }

    // Explicitly return public methods when this object is instantiated
    return{            
        getUserLocation : getCoords
    };

})(window);

在我的其他揭示模块模式(下面)中,我正在调用getUserLocation函数(也就是getCoords),并且当前,它调试用户位置正确(myLocation)。

// Revealing Modular pattern, containing functions relevant to park functionality    
var Park = (function(window, undefined){

    // Determine if user entered postcode or selected 'use my location'
    function handleUserData(data){

        if(data === "postcode"){
            var location = document.getElementById("postcode").value;
        }
        else{
            var location = Location.getUserLocation();
        }

    }

    // Explicitly return public methods when this object is instantiated
    return{            
        handleMe : handleUserData
    };

})(window); 

但是,当我更改console.log(myLocation)以返回myLocation,然后控制日志位置(在函数handleUserData中)时,我得到了未定义。

我的理论是,我希望获得用户位置,然后在Park揭示模块模式函数中使用它。

所以我的问题是,是否可以将一个显示模块模式的值返回到另一个?如果没有,我是否应该研究其他方法来帮助解决我的问题?

修改

从评论的帮助中,我能够使用以下方法解决此问题:

位置

// Revealing Modular pattern, containing functions relevant to location functionality    
var Location = (function(window, undefined){

    // Gets co-ordinates of users current position (TRUE: Run geoLocate function, FALSE: Run geoError function)
    function getCoords(callback){

        navigator.geolocation.getCurrentPosition(

            function(position){

                var lat = position.coords.latitude;
                var lng = position.coords.longitude;

                // Convert seperate variables into one string type variable (Google geocode expects string format)
                var myLocation = String(lat+","+lng);

                callback(myLocation);

            }

        )

    }

    // Explicitly return public methods when this object is instantiated
    return{            
        getUserLocation : getCoords
    };

})(window);

公园

// Revealing Modular pattern, containing functions relevant to park functionality    
var Park = (function(window, undefined){

    // Determine if user entered postcode or selected 'use my location'
    function handleUserData(data){

        var location;

        if(data === "postcode"){
            location = document.getElementById("postcode").value;
        }
        else{
            Location.getUserLocation(function(userLocation){
                location = userLocation;
                console.log(location);
            });
        }

    }

    // Explicitly return public methods when this object is instantiated
    return{            
        handleMe : handleUserData
    };

})(window);

0 个答案:

没有答案