[英]Shared callback handler in a javascript class
My current click handler. 我当前的点击处理程序。
let db = new DB()
$('#action-searchByPhone').click(() => {
db.searchByPhone("0400111221", (customers) => {
console.log(customers)
})
})
My current class. 我目前的课程。
I want to share the functionality of the success handler in this class like I am with the error handler. 我想像在错误处理程序中一样,在此类中共享成功处理程序的功能。 So I can share the success handler for various methods. 因此,我可以为各种方法共享成功处理程序。
class DB {
constructor() {
this.errorHandler = (tx, error) => console.log(error)
}
searchByPhone(phone, callback) {
let successHandler = (tx, result) => {
let records = Array.from(result.rows)
callback(records) // how do I pass this in?
}
this.db.transaction((tx) => {
tx.executeSql('SELECT * FROM customer WHERE phone = ?', [phone], successHandler, this.errorHandler)
})
}
}
This is what I have tried, but as you might expect it's not working. 这是我尝试过的方法,但是您可能希望它无法正常工作。 I am not sure though how I can pass the callback function into the success handler as well though. 我不确定如何将回调函数传递给成功处理程序。
class DB {
constructor() {
this.successHandler = (tx, result) => {
let records = Array.from(result.rows)
callback(records) // how do I pass this in?
}
this.errorHandler = (tx, error) => console.log(error)
}
searchByPhone(phone, callback) {
this.db.transaction((tx) => {
tx.executeSql('SELECT * FROM customer WHERE phone = ?', [phone], this.successHandler, this.errorHandler)
})
}
}
Use an
anonymous
arrow function in the executeSql success callback, which calls the successHandler with callback as an extra argument 在executeSql成功回调中使用
匿名
箭头函数,该函数将带有回调作为额外参数的successHandler调用
class DB {
constructor() {
this.successHandler = (tx, result, callback) => {
let records = Array.from(result.rows)
callback(records) // how do I pass this in?
}
this.errorHandler = (tx, error) => console.log(error)
}
searchByPhone(phone, callback) {
this.db.transaction((tx) => {
tx.executeSql('SELECT * FROM customer WHERE phone = ?', [phone], (tx, result) => this.successHandler(tx, result, callback), this.errorHandler)
})
}
}
I get the feeling the above could (should?) be written as: 我感觉以上内容可以(应该?)写为:
class DB {
constructor() {
}
successHandler(tx, result, callback) {
let records = Array.from(result.rows);
callback(records);
}
errorHandler(tx, error) {
console.log(error);
}
searchByPhone(phone, callback) {
this.db.transaction(tx => {
tx.executeSql('SELECT * FROM customer WHERE phone = ?', [phone], (tx, result) => this.successHandler(tx, result, callback), this.errorHandler)
});
}
}
You could also use bind, to bind callback as the first argument to successHandler 您还可以使用bind来将回调绑定为successHandler的第一个参数
class DB {
constructor() {
this.successHandler = (callback, tx, result) => {
let records = Array.from(result.rows)
callback(records) // how do I pass this in?
}
this.errorHandler = (tx, error) => console.log(error)
}
searchByPhone(phone, callback) {
this.db.transaction((tx) => {
tx.executeSql('SELECT * FROM customer WHERE phone = ?', [phone], this.successHandler.bind(this, callback), this.errorHandler)
})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.