0

I am having a problem with loosing scope inside a widget. Inside _initialize, this.options does not work, and neither does that.options. How can I access the scope of the widget from this method?

$.widget("myproj.map", {
        options: {
            centerLat: 51.511214,
            centerLong: -0.119824,
        },

        // the constructor
        _create: function () {
            var that = this;
            google.maps.event.addDomListener(window, 'load', this._initialize);
        },
        _initialize: function () {

            var mapOptions = {
                center: new google.maps.LatLng(that.options.centerLat, that.options.centerLong),
                zoom: 11,
            };

To clarify

$.widget("myproj.map", {
        options: {
            centerLat: 51.511214,
            centerLong: -0.119824,
        },

        // the constructor
        _create: function () {
            google.maps.event.addDomListener(window, 'load', this._initialize);
        },
        _initialize: function () {
            console.log(this);
            var mapOptions = {
                center: new google.maps.LatLng(this.options.centerLat, this.options.centerLong),
                zoom: 11
            }; ...code omitted

does not work, when I console.log this in _initialize I get

Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
2
  • you have commas all over the place... unended };, } etc... Commented Dec 5, 2013 at 0:32
  • 1
    I think he just omitted the closing of _initialize and widget , missing: } }); Commented Dec 5, 2013 at 0:36

2 Answers 2

1

google.maps.event.addDomListener changes what this refers to. you can change it back by calling the bind mehtod

google.maps.event.addDomListener(window, 'load', this._initialize.bind(this));
Sign up to request clarification or add additional context in comments.

Comments

0

EDIT: this answer does address rebinding caused by the event handler

var that is scoped inside the _create function, you should be able to access this from within initialize.

new google.maps.LatLng(this.options.centerLat, this.options.centerLong)

check out this similar question about "this"

2 Comments

I only added in the 'that' variable as I was loosing scope going into the initialize function from the google.maps.event listener
ahh i see. added a new answer

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.