Поддержание объема внутри виджета JQuery UI
У меня проблема с потерей видимости внутри виджета. Внутри _initialize, this.options
не работает и не работает that.options
, Как я могу получить доступ к области виджета из этого метода?
$.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,
};
Уточнить
$.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
не работает, когда я console.log это в _initialize я получаю
Window {top: Window, window: Window, location: Location, external: Object, chrome: Object…}
2 ответа
Решение
google.maps.event.addDomListener
что меняет this
относится к. вы можете изменить его обратно, вызвав метод bind
google.maps.event.addDomListener(window, 'load', this._initialize.bind(this));
РЕДАКТИРОВАТЬ: этот ответ выполняет привязку адреса, вызванную обработчиком события
var that
находится внутри _create
функция, вы должны иметь возможность доступа this
изнутри инициализировать.
new google.maps.LatLng(this.options.centerLat, this.options.centerLong)
проверить этот похожий вопрос об этом