【问题标题】:Update view model through ajax request through click binding in knockout通过 ajax 请求通过敲除中的单击绑定更新视图模型
【发布时间】:2014-02-27 23:27:28
【问题描述】:

我最初有一个与示例数据绑定的视图模型,绑定工作正常。

这是小提琴 http://jsfiddle.net/46LXU/

但是,如果我使用单击绑定从服务器获取相同的数据,我会获取数据,但我无法使用它来将其加载到视图模型。

<ul>
<li><a href="#" data-bind="click: function(data) { selectionChanged( 1 ) }">Sample Data 1</a></li>
<li><a href="#" data-bind="click: function(data) { selectionChanged( 2 ) }">Sample Data 2</a></li>
<li><a href="#" data-bind="click: function(data) { selectionChanged( 3 ) }">Sample Data 3</a></li>
</ul>

上面的每次点击都会通过以下 jQuery Post 从服务器加载新数据。

my.vm.selectionChanged = function (){
 //This loads new data from server
 jQuery.post(
      MyAjax.ajaxurl,
      {
          action : 'rcv_getpost',
          cvid : 2,
      },
      function( response ) {
           return {
             data: response 
            };
       }
    //Response now have the new data. How to load the data and to update it with view? 
    );      

my.vm.load();
}

我的实际视图模型

jQuery(document).ready(function ($) {


var my = { }; //my namespace

my.sampleData = (function (my) {    

    var dataary = {
  "sections": [
    {
      "section_name": "Qualifications",
      "data": "Sample Data",
      "key": "qual"
    },
    {
      "section_name": "Interests",
      "data": "These are my interests",
      "key": "int"
    },
    {
      "section_name": "Referance",
      "data": "This Is my referance",
      "key": "ref"
    }
  ]
};
     return {
             data: dataary
            };
})(my);

    // Product construction
    var Section = function () {
        this.section_name = ko.observable();
        this.data = ko.observable();
        this.key = ko.observable();
        this.prevent = ko.observable();
    };


    my.vm = {
        // observable array of sections
        sections: ko.observableArray([]),
        // loading the observable array with sample data
        load: function () {
            console.log(my.sampleData.data.sections);
            $.each(my.sampleData.data.sections, function (i, p) {
                    my.vm.sections.push(new Section()
                        .section_name(p.section_name)
                        .data(p.data)
                        .key(i)
                    )
            });
        }

    }; // End of My.VM


   my.vm.load();
   ko.applyBindings(my.vm);




});

【问题讨论】:

    标签: jquery ajax knockout.js


    【解决方案1】:

    在您的点击处理程序中:

      function( response ) {
           my.vm.load({
             data: response 
            });
       }
    

    并删除对load() 的另一个调用。请记住,AJAX 操作是异步的;他们的回调不能返回值。

    在您的虚拟机中:

        load: function (data) {
            console.log(data);
            my.vm.sections([]); // assuming you want to replace your sample data rather than append to it
            $.each(data.sections, function (i, p) {
    

    最后:

    my.vm.load(my.sampleData);
    ko.applyBindings(my.vm);
    

    【讨论】:

    • 我就是想不通,jsfiddle.net/46LXU/2我哪里做错了?
    • 你有一个语法错误(不匹配的);),由于某种原因,JQuery 在小提琴上下文中抛出错误,使得调试变得非常困难。修复语法错误并尝试使用旧版本的 jQuery 设置小提琴。
    • 我让它在我的应用程序上运行,遵循你的方法。但是我无法让它在 Jsfiddle 上运行。 doc.jsfiddle.net/use/echo.html 我想我在搞乱 jsfiddle 的 Ajax API 请求
    • AJAX问题很可能是同源策略的结果;从 jsfiddle 加载的代码无法向其他站点发出 AJAX 请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多