2

如何在 jQuery 函数中调用 viewmodel 函数?我只想从 Javascript 函数中调用 viewmodel 函数的函数。

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();
    }
  }
}

如何changeNumberjscroll窗格功能调用?

$('.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]);   


    }
});
}

提前致谢。

4

3 回答 3

1

我同意 Anders 关于使用自定义绑定的观点。但是,如果您真的想这样做,请尝试从您的 ContactsViewModel 返回“自我”(参见下面的示例)

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);
于 2013-01-16T04:29:06.303 回答
0

创建一个连接 jQuery 内容的自定义绑定。你的视图模型中不应该有任何与 DOM 相关的代码,它是一种反模式。

类似的东西(不工作的代码)

ko.bindingHandlers.scrollable = {
   init: function(element, valueAccessor) {
      var onScroll = valueAccessor();
      $(element).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) {
           onScroll();
        }
      });
   }
};

然后从你的角度使用

<div data-bind="scrollable: changeNumber"></div>
于 2013-01-15T11:44:58.080 回答
0

你可以这样做:


视图模型

var searchViewModel = function () {
    var self = this;

    self.search = function (param) {
        //do something
    };
}

HTML

<button data-bind="click: function () { Search() }" class="" id="searchBtn">Search</button>

jQuery

function Search() {
    // paramvalue = 1; 
    viewModel.search(paramvalue);                  
}
于 2015-04-21T09:15:23.083 回答