weixin_33686714 2014-10-27 00:21 采纳率: 0%
浏览 53

jQuery调用敲除方法

For some reason I want to call knockout method in jQuery.

Knockout viewModel is already binding. I don't know how to call it in jQuery.

Here is my code.

$(document).ready() {
  form_submit();
}

function form_submit() {
  var form = $('#form');
  var serializedData = form.serialize();
  $.get("{% url 'search:search' %}", serializedData, function(response) {
    AppViewModel.count(response.count); // I tried this, it says undefined is not a function
    //Then I tried:
    var newModel = new AppViewModel();
    newModel.count(response.count); //No errors, but UI doesn't change. I know it's because I'm not binding it. But I don't think I can binding it, because only 1 ko.binding allowed.
}

function AppViewModel() {
   var self = this;
   self.count = ko.observable(count); //Assume I initially assigned some value to count.
   //some other code below not related to this question.
}

ko.applyBindings(new AppViewModel());

Hope someone can help. Thanks!

  • 写回答

1条回答 默认 最新

  • weixin_33728708 2014-10-27 00:45
    关注

    Why don't you assign the result of calling new AppViewModel to a variable before applying the bindings on it? Then you can reference that variable inside your jQuery get callback.

    Something like the following:

    $(document).ready(function() {
        form_submit();
    });
    
    var vm = new AppViewModel();
    
    function form_submit() {
        var form = $('#form');
        var serializedData = form.serialize();
        $.get("{% url 'search:search' %}", serializedData, function(response) {
            vm.count(response.count);
        });
    }
    
    function AppViewModel() {
       var self = this;
       this.count = ko.observable(count);
    }
    
    
    ko.applyBindings(vm);
    

    You can also just bind to your form's submit event which should simplify some of your code organization.

    For example, in the following HTML, I'm binding to my form's submit event:

    <div id="myHTML">
        <form data-bind="submit: myFormHandler">
            <!-- form inputs here -->
        </form>
    </div>
    

    Then in my view model, I'd have the following:

    function AppViewModel() {
        this.count = ko.observable();
    
        this.myFormHandler = function(formElement) {
            var self = this;
            var serializedData = $(formElement).serialize();
            $.get("{% url 'search:search' %}", serializedData, function(response) {
                self.count(response.count);
            });
        }
    }
    
    $(document).ready(function() {
        var vm = new AppViewModel();
    
        ko.applyBindings(vm, $("#myHTML")[0]);
    });
    

    Note that in the above example, I'm scoping my bindings to the myHTML div.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘