敲除绑定 - 使用 javascript 个对象但不使用 json 个对象
knockout binding - working with javascript objects but not json object
我正在尝试通过研究现有的源代码并对它们进行小的更改来学习 knockoutjs。我在这里尝试理解的源代码是 RP Niemeyer 的 knockout-sortable.js 示例。
原示例代码为here.
目的是能够在 json 字符串或 jtoken 对象中从服务器获取数据并在代码中使用它..
我对它做了一个小改动并做了一个 console.log 得到 json here
var initialTables = [
new Table("Table One", [
new Student(1, "Bobby", "male"),
new Student(2, "Ted", "male"),
new Student(3, "Jim", "male")
]),
new Table("Table Two", [
new Student(4, "Michelle", "female"),
new Student(5, "Erin", "female"),
new Student(6, "Chase", "male")
]),
new Table("Table Three", [
new Student(7, "Denise", "female"),
new Student(8, "Chip", "male"),
new Student(9, "Kylie", "female")
]),
new Table("Table Four", [
new Student(10, "Cheryl", "female"),
new Student(11, "Doug", "male"),
new Student(12, "Connor", "male")
]),
new Table("Table Five", [
new Student(13, "Cody", "male"),
new Student(14, "Farrah", "female"),
new Student(15, "Lyla", "female")
])
];
this.tables = ko.observableArray(initialTables);
initialTables 对象使用 console.log(ko.toJSON(this.tables))
转换为 json,可以在 console.log 中看到,如下所示。
[{"students":[{"id":1,"name":"Bobby","gender":"male"},{"id":2,"name":"Ted","gender":"male"},{"id":3,"name":"Jim","gender":"male"}]},{"students":[{"id":4,"name":"Michelle","gender":"female"},{"id":5,"name":"Erin","gender":"female"},{"id":6,"name":"Chase","gender":"male"}]},{"students":[{"id":7,"name":"Denise","gender":"female"},{"id":8,"name":"Chip","gender":"male"},{"id":9,"name":"Kylie","gender":"female"}]},{"students":[{"id":10,"name":"Cheryl","gender":"female"},{"id":11,"name":"Doug","gender":"male"},{"id":12,"name":"Connor","gender":"male"}]},{"students":[{"id":13,"name":"Cody","gender":"male"},{"id":14,"name":"Farrah","gender":"female"},{"id":15,"name":"Lyla","gender":"female"}]}]
现在我将 initialTables 更改为我之前记录的 json 对象并调用表如下所示和 here
this.tables = ko.observableArray(ko.mapping.fromJS(initialTables));
现在,即使我没有看到任何错误,我也没有在结果窗格中看到 initialTables 对象。
我做错了什么??
真诚感谢所有帮助..
谢谢
好吧 arnab
你缺少大括号()
应该可以完成你的工作。
请在此行进行修改
ko.observableArray(ko.mapping.fromJS(initialTables))
类似于
ko.observableArray(ko.mapping.fromJS(initialTables)())
你需要将 Array
传递给可观察数组。如果你在最后给出 ()
你会得到一个数组(展开内容)。
工作fiddlehere
我正在尝试通过研究现有的源代码并对它们进行小的更改来学习 knockoutjs。我在这里尝试理解的源代码是 RP Niemeyer 的 knockout-sortable.js 示例。
原示例代码为here.
目的是能够在 json 字符串或 jtoken 对象中从服务器获取数据并在代码中使用它..
我对它做了一个小改动并做了一个 console.log 得到 json here
var initialTables = [
new Table("Table One", [
new Student(1, "Bobby", "male"),
new Student(2, "Ted", "male"),
new Student(3, "Jim", "male")
]),
new Table("Table Two", [
new Student(4, "Michelle", "female"),
new Student(5, "Erin", "female"),
new Student(6, "Chase", "male")
]),
new Table("Table Three", [
new Student(7, "Denise", "female"),
new Student(8, "Chip", "male"),
new Student(9, "Kylie", "female")
]),
new Table("Table Four", [
new Student(10, "Cheryl", "female"),
new Student(11, "Doug", "male"),
new Student(12, "Connor", "male")
]),
new Table("Table Five", [
new Student(13, "Cody", "male"),
new Student(14, "Farrah", "female"),
new Student(15, "Lyla", "female")
])
];
this.tables = ko.observableArray(initialTables);
initialTables 对象使用 console.log(ko.toJSON(this.tables))
转换为 json,可以在 console.log 中看到,如下所示。
[{"students":[{"id":1,"name":"Bobby","gender":"male"},{"id":2,"name":"Ted","gender":"male"},{"id":3,"name":"Jim","gender":"male"}]},{"students":[{"id":4,"name":"Michelle","gender":"female"},{"id":5,"name":"Erin","gender":"female"},{"id":6,"name":"Chase","gender":"male"}]},{"students":[{"id":7,"name":"Denise","gender":"female"},{"id":8,"name":"Chip","gender":"male"},{"id":9,"name":"Kylie","gender":"female"}]},{"students":[{"id":10,"name":"Cheryl","gender":"female"},{"id":11,"name":"Doug","gender":"male"},{"id":12,"name":"Connor","gender":"male"}]},{"students":[{"id":13,"name":"Cody","gender":"male"},{"id":14,"name":"Farrah","gender":"female"},{"id":15,"name":"Lyla","gender":"female"}]}]
现在我将 initialTables 更改为我之前记录的 json 对象并调用表如下所示和 here
this.tables = ko.observableArray(ko.mapping.fromJS(initialTables));
现在,即使我没有看到任何错误,我也没有在结果窗格中看到 initialTables 对象。
我做错了什么??
真诚感谢所有帮助..
谢谢
好吧 arnab
你缺少大括号()
应该可以完成你的工作。
请在此行进行修改
ko.observableArray(ko.mapping.fromJS(initialTables))
类似于
ko.observableArray(ko.mapping.fromJS(initialTables)())
你需要将 Array
传递给可观察数组。如果你在最后给出 ()
你会得到一个数组(展开内容)。
工作fiddlehere