如何通过“可观察量”创建(“映射”)复杂类型与 Knockout.js 一起使用吗?

发布于 2024-12-23 13:16:14 字数 931 浏览 1 评论 0原文

因此,我正在学习 knockout.js,并且我对如何在其中创建嵌套复杂类型感到有点困惑。

例如,在我的服务器端,我的模型是:

class Person {
public string Name {get; set;}
public int Age {get; set;}
public List<Colors> FavoriteColors {get; set;}
}

class Color {
public int ColorId {get; set;}
public string Name {get; set;}
}

asp.net mvc 输出的 JSON 类似于(如果我输出 List 类型):

[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},
{"Name":"Albert","Age":29,"Colors":{"ColorId":2,"Name":"Blue"}}]

现在我想通过映射它observables,通过 Jquery Ajax 获取。我知道 FavoriteColors 将是一个数组,但我有点困惑这里的情况如何......

任何帮助将不胜感激!

更新:

有人可以帮我解决这个问题吗? (我做了一个原型,但我的选择不起作用)

http://jsbin .com/eqihun/3/edit#javascript,html,live

So, I'm learning knockout.js and I am a bit stumped on how to create nested complex types in it.

For example, on my server-side, my model is:

class Person {
public string Name {get; set;}
public int Age {get; set;}
public List<Colors> FavoriteColors {get; set;}
}

class Color {
public int ColorId {get; set;}
public string Name {get; set;}
}

The JSON that asp.net mvc outputs is something like (if I output a List<Person> type):

[{"Name":"JC","Age":24,"Colors":[{"ColorId":1,"Name":"Red"},{"ColorId":2,"Name":"Blue"}]},
{"Name":"Albert","Age":29,"Colors":{"ColorId":2,"Name":"Blue"}}]

Now I want to map that through observables, acquired through Jquery Ajax. I know that FavoriteColors would be an array, but I'm a bit confused how things would workout here...

Any help would be greatly appreciated!

UPDATE:

can anyone help me with this? (i made a prototype, but my select doesn't work)

http://jsbin.com/eqihun/3/edit#javascript,html,live

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2024-12-30 13:16:14

看看 knockout 映射插件

编辑:您的示例已编辑:http://jsbin.com/eqihun/18/edit

take a look at the knockout mapping plugin

edit: your sample edited: http://jsbin.com/eqihun/18/edit

挖个坑埋了你 2024-12-30 13:16:14

解决方案链接:此处

$(document).ready(function(){

  //document.writeln(data[0].Colors[0].Name);

  //if I map anything to ko.observable, it would be sent through ko.toJSON... so that means that Color stuff should NOT be mapped, especially because that's not what MVC is asking, but rather List<Colors>...

  var Color = function (id, name) {
     var self = this;
     self.colorId = id;
     self.name = name;
  };

 function viewModel() {
    var self = this;
    self.Name = ko.observable("Bert");
    self.Age = ko.observable('12');
    self.FavoriteColors = ko.observableArray([
      new Color(1, "Blue"), 
      new Color(2, "Red"), 
      new Color(3, "White")
    ]);
  } 
  ko.applyBindings(new viewModel());    
});

HTML:

<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script src="https://raw.github.com/SteveSanderson/knockout/master/build/output/knockout-latest.debug.js" type="text/javascript"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>

  <p>Name: <input type="text" data-bind="value: Name" name="Name"></p>
  <p>Age: <input type="text" data-bind="value: Age" name="Name"></p>


  <select data-bind="options: FavoriteColors, optionsText: 'name', value: 'colorId', optionsCaption: 'Choose...'"></select>

link to solution: here

$(document).ready(function(){

  //document.writeln(data[0].Colors[0].Name);

  //if I map anything to ko.observable, it would be sent through ko.toJSON... so that means that Color stuff should NOT be mapped, especially because that's not what MVC is asking, but rather List<Colors>...

  var Color = function (id, name) {
     var self = this;
     self.colorId = id;
     self.name = name;
  };

 function viewModel() {
    var self = this;
    self.Name = ko.observable("Bert");
    self.Age = ko.observable('12');
    self.FavoriteColors = ko.observableArray([
      new Color(1, "Blue"), 
      new Color(2, "Red"), 
      new Color(3, "White")
    ]);
  } 
  ko.applyBindings(new viewModel());    
});

HTML:

<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  <script src="https://raw.github.com/SteveSanderson/knockout/master/build/output/knockout-latest.debug.js" type="text/javascript"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>

  <p>Name: <input type="text" data-bind="value: Name" name="Name"></p>
  <p>Age: <input type="text" data-bind="value: Age" name="Name"></p>


  <select data-bind="options: FavoriteColors, optionsText: 'name', value: 'colorId', optionsCaption: 'Choose...'"></select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文