firefox 说 `this.view` 未定义,但在控制台中转储对象时明确定义
firefox says `this.view` is undefined, but its clearly defined when dumping object in console
我读了一篇关于通过创建单例来构建单例的文章,然后覆盖它们的构造函数并始终返回第一个创建的实例。这工作正常,但是我的方法之一无法引用它的属性:
//fetch a view and render it with the supplied args, then perform a callback.
jqMVC.prototype.render = function(template,args,callback){
twig({
href: this.view_path+template,
load: function(template) {
var html = template.render(args);
this.view.html(html).promise().done(function(){
if(typeof callback === "function"){
callback();
}
});
}
});
};
正如您在图像中看到的那样,视图被明确定义,如果程序员没有覆盖它,甚至还有一个默认值。视图是一个 jquery 对象。我如何在我的方法中使用它?
问题出在 twig
回调中 this
可能没有引用 jqMVC
对象。
您可以使用下面给出的闭包变量
jqMVC.prototype.render = function (template, args, callback) {
var self = this;
twig({
href: this.view_path + template,
load: function (template) {
var html = template.render(args);
self.view.html(html).promise().done(function () {
if (typeof callback === "function") {
callback();
}
});
}
});
};
我读了一篇关于通过创建单例来构建单例的文章,然后覆盖它们的构造函数并始终返回第一个创建的实例。这工作正常,但是我的方法之一无法引用它的属性:
//fetch a view and render it with the supplied args, then perform a callback.
jqMVC.prototype.render = function(template,args,callback){
twig({
href: this.view_path+template,
load: function(template) {
var html = template.render(args);
this.view.html(html).promise().done(function(){
if(typeof callback === "function"){
callback();
}
});
}
});
};
正如您在图像中看到的那样,视图被明确定义,如果程序员没有覆盖它,甚至还有一个默认值。视图是一个 jquery 对象。我如何在我的方法中使用它?
问题出在 twig
回调中 this
可能没有引用 jqMVC
对象。
您可以使用下面给出的闭包变量
jqMVC.prototype.render = function (template, args, callback) {
var self = this;
twig({
href: this.view_path + template,
load: function (template) {
var html = template.render(args);
self.view.html(html).promise().done(function () {
if (typeof callback === "function") {
callback();
}
});
}
});
};