Reputation: 757
When I use a variable in javascript, it gives an error
ReferenceError: getalletje is not defined
, depending on where I declare it.
This is where I declare the variable:
get_name: function(){
var getalletje = 5;
return getalletje;
},
Where I'm trying to use the variable:
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
It gives an error like this.
But: If I put var getalletje = 5;
just above self.$('.geldinuit-button').click(function(){
, it works.
Something extra I need to do?
Edit for Shomz: This is the full code:
.............
self.set_smart_status(status.newValue);
});
this.$el.click(function(){
self.pos.connect_to_proxy();
});
},
});
module.PosWidget = module.PosBaseWidget.extend({
template: 'PosWidget',
init: function() {
this._super(arguments[0],{});
this.pos = new module.PosModel(this.session,{pos_widget:this});
this.pos_widget = this; //So that pos_widget's childs have pos_widget set automatically
.............................
............................
get_name: function(){
var getalletje = 5;
return getalletje;
},
...........................
...........................
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert( this.get_name() );
},
});
});
..........................
Upvotes: 1
Views: 101
Reputation: 2708
you can't access variable outside of the function you declare it in. If you want to access variable in a global way you should declare it outside of get_name. In your case I don't see the point but the following should work:
var getalletje = 0;
get_name: function(){
getalletje = 5;
return getalletje;
},
self.$('.geldinuit-button').click(function(){
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
also, shouldn't:
comment: _t('getalletje'),
become
comment: _t(getalletje),
UPDATE
Also, in your case this declaration isn't useful. I don't know if you changed your code when asking your question but in this case doing only:
self.$('.geldinuit-button').click(function(){
var getalletje = 5;
self.screen_selector.show_popup('geldinuit',{
message: _t('Popup titel'),
comment: _t('getalletje'),
confirm: function(){
window.alert(getalletje);
},
});
});
Upvotes: 0
Reputation: 37711
It's a scoping issue - the variable you declare is available only locally. To get its value, you can do something like:
confirm: function(){
window.alert( yourObject.get_name() );
},
where yourObject
is the object that you defined the get_name
method for.
Upvotes: 1