javascript - How to pass object context to .on() callback function -
i'm working on structuring javascript using module pattern explained here.
this simplified version of code:
var obj = { binduiactions: function(){ $('#button').on('click', {self: this}, this.dostuff); }, dostuff: function(e){ $(this).fadeout(); var self = e.data.self; self.dosomethingelse(); }, dosomethingelse: function(){ //.... } }
i need pass object context dostuff method because don't want use:
obj.dosomethingelse();
so can use code again somewhere else, or change object name ('obj') later on. using event data not feel best solution, there better way done?
i've used $.proxy change context .on() callback function , works. problem need keep context click-element also,
$(this).fadeout();
to work.
thanks in advance!
i think set context 1 of these:
- native: function.bind (js 1.8+)
- jquery function: $.proxy
- third party libs underscore: _.bind
Comments
Post a Comment