I am new to backbonejs. I am trying to pass the correct this object to a callback function where that function is a method of the view.
My current solution:
APP.LocationFormView = APP.View.extend({
initialize: function () {
if (navigator.geolocation) {
var that = this;
var success = function(position) {
_.bind(that.onSuccessUpdatePos, that, position)();
};
var error = function(error) {
_.bind(that.onFailUpdatePos, that, error)();
}
navigator.geolocation.getCurrentPosition(success,
error);
} else {
}
},
onSuccessUpdatePos: function(position) {
// We can access "this" now
},
onFailUpdatePos : function(error) {
// We can access "this" now
}
});
Is this a correct way to achieve what I want?
Is there any less verbose solution for this?
This is how I would do it. One nice aspect of
bindAllis that if you add additional functions toLocationFormViewthey will automatically havethisbound.