简体   繁体   English

Javascript-如何在ajax调用中将“ this”绑定到对象文字

[英]Javascript - how to bind 'this' inside an ajax call to the object literal

I have an object literal router , containing an ajax call. 我有一个对象字面量router ,其中包含ajax调用。 I want to call other functions this.printMovies() inside the ajax call but this refer to the ajax object. 我想打电话给其他功能this.printMovies() Ajax调用内部,但是this指的是Ajax对象。

How do I escape it and make this refer to the router object itself? 我该如何逃生呢,让this指的是router对象本身?

var router = {  

    //...
    init : function() {
        this.getData("api/movies", "movies", callback);
    },
    getData : function (url, htmlType, callback) {
        $.ajax({
            url: url,
            dataType: 'json',
            success: function (response) {
                if (response && response.length > 0) {
                    this.printMovies(response, callback); //'this' refers to ajax
                    this.printMovies(response, callback).bind(this) //still doesn't work
                }
            },
            error: function (response) { console.log("Error:" + response); }
        });
    },
    printMovies : function(){

    },  
}

Pass context option to ajax: context选项传递给ajax:

$.ajax({
  context: this,
  /* other options */
}

Now inside ajax callbacks, this will refer to router object. 现在在ajax回调内部, this将指向router对象。

Here in this case, the function getData holds the context of it's parent object in this keyword. 在这种情况下,此函数getDatathis关键字中保存其父对象的上下文。 So what you can do is, store the reference of this in some variable and use it later. 因此,您可以做的是,将this的引用存储在某个变量中,并在以后使用。 like: 喜欢:

var router = {  

    //...
    init : function() {
        this.getData("api/movies", "movies", callback);
    },
    getData : function (url, htmlType, callback) {
        var mainObj = this; // line to be noticed

        $.ajax({
            url: url,
            dataType: 'json',
            success: function (response) {
                if (response && response.length > 0) {
                    // parent object to be used
                    mainObj.printMovies(response, callback); //'this' refers to ajax
                }
            },
            error: function (response) { console.log("Error:" + response); }
        });
    },
    printMovies : function(){

    }
}

Bind the entire success call back with bind it will work: 将整个成功调用与bind绑定将起作用:

(function (response) {
            if (response && response.length > 0) {
                this.printMovies(response, callback);                                     }
        }).bind(this)

You can use the new ES6 arrow functions , or bind . 您可以使用新的ES6 箭头功能bind

You might have to do this on your success or getData function 您可能必须在成功或getData函数上执行此操作

getData : function (url, htmlType, callback) {
  ...
}.bind(this),

A very common approach is to assign this to a local variable at the beginning of your function. 一个非常常见的方法是指定this在你的函数开头的局部变量。

var self = this;

Then inside your callback use self instead of this : 然后在回调中使用self代替this

self.printMovies(response, callback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM