我无法从Post.success函数中调用this.RenderDeals().我认为这与范围有关?有人可以解决这个问题,并可能建议一个解决方法.我尝试过使用原型和模块模式,两者都没有运气.
FYI Post是$ .ajax包装器,它返回一个jQuery Deferred对象.
function Deals() {
this.template = '#trTemplate';
this.container = '#containerTable';
this.GetDeals = function () {
Post('Deal.svc/GetDeals')
.success(function (result) {
this.RenderDeals(result);
});
};
this.RenderDeals = function (deals) {
$(this.template).tmpl(deals).appendTo(this.container);
}
}
var deal = new Deals();
deal.GetDeals();
Run Code Online (Sandbox Code Playgroud)
更新:
好的,所以我在GetDeals函数上方添加了以下行:
var me = this;
Run Code Online (Sandbox Code Playgroud)
而是打电话
me.RenderDeals(result);
Run Code Online (Sandbox Code Playgroud)
似乎工作正常,但我不知道为什么.
this您的"成功"功能中的参考几乎肯定不是您认为的那样.试试这个改变:
function Deals() {
var instance = this;
this.template = '#trTemplate';
this.container = '#containerTable';
this.GetDeals = function () {
Post('Deal.svc/GetDeals')
.success(function (result) {
instance.RenderDeals(result);
});
};
this.RenderDeals = function (deals) {
$(this.template).tmpl(deals).appendTo(this.container);
}
}
Run Code Online (Sandbox Code Playgroud)
通过this在"实例"变量中存储引用,您的处理程序将有一种保证的方式在调用其他函数("RenderDeals")时返回相关实例.
| 归档时间: |
|
| 查看次数: |
592 次 |
| 最近记录: |