我是backbonejs的新手.我试图将正确的this对象传递给回调函数,其中该函数是视图的方法.
我目前的解决方案
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
}
});
Run Code Online (Sandbox Code Playgroud)
这是实现我想要的正确方法吗?对此有没有更简洁的解决方案?