gpt4 book ai didi

javascript - Promise.resolve()/Backbone 内的上下文

转载 作者:行者123 更新时间:2023-12-03 07:33:15 25 4
gpt4 key购买 nike

我目前正在尝试使用 Promise 来解决从数据库获取模型的问题。以下是:

Promise.resolve(app.Departments.fetch()).then(function(response){
console.log(response);
this.$el.html( this.template( {depts: app.Departments.toJSON()} ));
this.$form = this.$('#form-employee');
this.$form.validator();
return this;
})

考虑一切都在 render 方法内部,并且该方法位于 Backbone.View.extend({}) 对象内部。问题是,在 Promise.resolve() 函数内部, this 的上下文与 View 对象内部的上下文不同,它会抛出一个错误,不知道 this 指的是什么。是否有办法将 this 的正确上下文传递给 Promise.resolve?

最佳答案

为此使用本地引用:

var self=this;
Promise.resolve(app.Departments.fetch()).then(function(response){
console.log(response);
self.$el.html( self.template( {depts: app.Departments.toJSON()} ));
self.$form = this.$('#form-employee');
self.$form.validator();
return self;
})

关于javascript - Promise.resolve()/Backbone 内的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35725995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com