【发布时间】:2017-02-26 14:54:20
【问题描述】:
main 函数如下,会导致 'setState' is not a valid property 异常。
componentWillMount = function () {
api.daysOfMonth(this, this.props.month, this.props.year, function (ds) {
var days = [];
ds.forEach(function (jsonDay) {
var day = {date: jsonDay.date, inRange: jsonDay.inRange};
days.push(day);
});
this.setState({ daysOfMonth: days });
});
api.js 的 sn-p 如下。 主函数调用 Api.daysOfMonth(...),Api.daysOfMonth 将使用全局对象调用 ajax 方法并使用协议 apply 调用回调函数,回调函数从 main 函数传入如上脚本。
ApiImpl = (function () {
function ApiImpl() { }
ApiImpl.prototype.invoke = function (callerObj, callback, action) {
var params = [];
for (var _i = 3; _i < arguments.length; _i++) {
params[_i - 3] = arguments[_i];
}
params.push(callback); //push callback function into params, so object of QWebChannel can callback the function after execute the 'action'
if (typeof window['api'] === 'undefined') {
new QWebChannel(qt.webChannelTransport, function (channel) {
window['api'] = channel.objects.api;
var func = window['api'][action].bind(callerObj);
return func.apply(callerObj, params); //here goes error
});
}
var func = window['api'][action].bind(callerObj);
return func.apply(callerObj, params); //here goes error
};
return ApiImpl;
}());
Api = (function () {
function Api() {
}
Api.daysOfMonth = function (callerObj, month, year, callback) {
this.impl.invoke(callerObj, callback, 'daysOfMonth', month, year);
};
return Api;
}());
Api.impl = new ApiImpl();
exports_1("Api", Api);
【问题讨论】:
-
您希望
this成为什么?jsonDay?
标签: javascript callback this