【问题标题】:Ajax data binding using Knockout Js使用 Knockout Js 的 Ajax 数据绑定
【发布时间】:2016-12-09 08:12:09
【问题描述】:

我正在使用 knockout js,我发现很难绑定数据,而在 ajax get 方法中,我已经创建了模型、viewModel 和 ajax 函数,我有同样的 ajax 方法js 文件,我在其中创建了 viewModel 我在页面加载时调用 ajax 并尝试将我的 html 与 konckout js 绑定,如果我在 ajax 调用之前给出 this.name = ko.observale(result[0].name) ,我会收到错误 this.name = ko.observale(result[0].name),在 ajax 调用它之后给出name is undefined需要帮助

<html>
  <head>
    <script src="js/jquery1.9.js"></script>
    <script src="js/knockout-3.3.0.js"></script>
    <script src="js/knockout.mapping.js"></script>
    <script src="model/usermodel.js"></script>
  </head>

  <body>

    <div>
      <h1><span data-bind="text:user().name"></span></h1>
      <h1><span data-bind="text:user().userName"></span></h1>
    </div>
    <script src="ViewModel/userDetailsViewModel.js"></script>
  </body>
</html>
////Model////
function userModel(result) {
  var self = this;
  this.name = ko.observable(result[0].name); /// give me error undefined before  the ajax call and after ajax call i get the value in result
  this.userName = ko.observable();

}

/////View Model////
var result
var userDetailsViewModel = function(result) {
  self = this;
  self.user = ko.observable(new userModel(result));
};
var mainUserDetailsViewModel = new userDetailsViewModel(result);
ko.applyBindings(mainUserDetailsViewModel);

////ajax called on the page load ////
$.ajax({
  type: "POST",
  dataType: "json",
  url: baseUrl + 'api/xx/xxx',
  data: jason.strigfy(),
  success: function(data) {
    result = data;
    ////I am getting in result json data object 0=["name":"nnnn","Username":"mmmmmm"],
    ////  i am passing this result to ViewModel and to Usermodel Constructor//
    mainUserDetailsViewModel.user(new userModel(result));

  },
  error: function(error) {
    jsonValue = jQuery.parseJSON(error.responseText);
    //jError('An error has occurred while saving the new part    source: ' + jsonValue, { TimeShown: 3000 });
  }
});

【问题讨论】:

    标签: javascript jquery knockout.js


    【解决方案1】:

    这是我的建议,即拥有一个干净的嵌套视图模型。
    示例:https://jsfiddle.net/kyr6w2x3/28/

    function UserViewModel() {
        var self = this;
        self.UsersList = ko.observableArray([]);
    
        self.GetUsers = function() {
          $.ajax({
            type: "POST",
            dataType: "json",
            url: baseUrl + 'api/xx/xxx',
            data: jason.strigfy(),
            success: function (data) {
                //Here you map and create a new instance of userDetailVM
                self.UsersList($.map(data, function (user) {
                   return new UserDetailViewModel(user);
              }));
            }
          });
        }
       //call to get users list when the VM is loading or you can call it on any event on your model
       self.GetUsers();
    }
    function UserDetailViewModel(data){
        var self = this;
       self.Name = ko.observable(data.name);
       self.UserName = ko.observable(data.username);
    }
    
    ko.applyBindings(new UserViewModel()); 
    

    查看:

     <h1 data-bind="foreach: UsersList">
        <div data-bind="text: Name"></div>
        <div data-bind="text: UserName"></div>
     </h1>
    

    【讨论】:

    • jason.strigfy()?
    • @ScottBeeson 这是我没碰过的他的 sn-p 代码的一部分!
    • 但是为什么它在 jsfiddle 上似乎运行良好?我很困惑:)
    • 在 jsfiddle 上,ajax 在那里,但从未被调用。相反,有一个地图功能可以完成这项工作!阅读评论!
    猜你喜欢
    • 2016-12-08
    • 2017-06-19
    • 2015-03-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2018-07-18
    • 1970-01-01
    相关资源
    最近更新 更多