我有一个视图模型,它有不同的方法可以在ajax上工作。我想使用这个库spin.js在调用ko.applyBindings()期间应用视图模型的同一元素上显示微调器。我没有看到任何允许在我的视图模型中检索dom元素的Knockout.js方法,我想知道是否有一种方法可以获取它。我确信这在某种程度上超越了MVVM的纯度,所以如果有其他方法可以做到这一点,请告诉我。
我可以将dom元素作为参数传递给我的视图模型,但是现在我想使用的每个视图模型都必须跟踪该元素,这是多余的,因为Knockout已经在跟踪它了。
//启动脚本
var _paymentMethodViewModel;
$(function () {
_paymentMethodViewModel = new ViewModels.PaymentMethodViewModel("/Customer");
ko.applyBindings(_paymentMethodViewModel, $("#PaymentMethodsList").get(0));
// spinner should show up over the PaymentMethodsList dom element when this is called and when the ajax call is complete it should disappear
_paymentMethodViewModel.getAllPaymentMethods();
});//查看模型
self.getAllPaymentMethods = function () {
// ******************
// start spinner here
// ******************
var spinner = new Spinner(_spinnerOpts).spin(/* <need dom element here> */);
$.ajax({
url: self.rootUrl + "/GetAllPaymentMethods",
type: "POST",
data: {},
context: self,
success: function (result, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
},
complete: function (jqXHR, textStatus) {
// ******************
// stop spinner here
// ******************
spinner.stop();
}
});
}发布于 2012-10-10 05:09:09
好了,我想出了一个我很满意的自定义绑定处理程序解决方案。
http://jsfiddle.net/StrandedPirate/UbFZT/
https://stackoverflow.com/questions/12805629
复制相似问题