调用jQuery中的剔除viewmodel函数 [英] Call knockout viewmodel function in jQuery
本文介绍了调用jQuery中的剔除viewmodel函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在jQuery函数中调用viewmodel函数?我只想从Javascript函数中调用viewmodel函数.
How to call a viewmodel function in a jQuery function? I just want to call a function of viewmodel function from a Javascript function.
function ContactsViewModel(data) {
var self = this;
// Editable data
self.Contacts = ko.observableArray(JSON.parse(data));
self.limit = ko.observable(20);
self.changeNumber = function(item){
self.limit(self.limit()+20);
self.Contacts.push(item);
}
self.myPostProcessingLogic = function(elements) {
if ($('#KnockOutContacts').children().length === ko.toJS(self.Contacts).length) {
// Only now execute handler
jq();
}
}
}
如何从jscroll
窗格功能调用changeNumber
?
$('.jspScrollable').bind(
'jsp-arrow-change',
function(event, isAtTop, isAtBottom, isAtLeft, isAtRight) {
// Now look at the is* parameters and do what you
// need to do. All four of the is* parameters are booleans.
if(isAtBottom) {
ContactsViewModel.changeNumber();
}
}
);
数据来自服务器
function returnData(url,data,type){
$.post(url, data, function(returnedData) {
if(type == "contacts")
{
ko.applyBindings(new ContactsViewModel(returnedData),$("#KnockOutContacts")[0]);
}
else if(type == "logs")
{
ko.applyBindings(new LogsViewModel(returnedData),$("#KnockOutLogs")[0]);
}
else if(type == "sms")
{
ko.applyBindings(new SmsViewModel(returnedData,"#KnockOutSms"),$("#KnockOutSms")[0]);
ko.applyBindings(new SmsViewModel(returnedData,"#KnockOutSmsData"),$("#KnockOutSmsData")[0]);
}
});
}
谢谢.
推荐答案
我同意Anders关于使用自定义绑定的知识.但是,如果您确实愿意,请尝试从ContactsViewModel返回自我"(请参见下面的示例)
I agree with Anders about using custom bindings. But if you really want to, try returning 'self' from your ContactsViewModel (see example below)
function ContactsViewModel(data) {
var self = this;
// Editable data
self.Contacts = ko.observableArray(JSON.parse(data));
self.limit = ko.observable(20);
self.changeNumber = function(item){
self.limit(self.limit()+20);
self.Contacts.push(item);
}
self.myPostProcessingLogic = function(elements) {
if ($('#KnockOutContacts').children().length === ko.toJS(self.Contacts).length) {
// Only now execute handler
jq();
}
}
//return self
return self;
}
//Variable you'll use to reference in jQuery
var myVm = ContactsViewModel(yourdata);
ko.applyBindings(myVm);
myVm.changeNumber(yourItem);
这篇关于调用jQuery中的剔除viewmodel函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文