In knockoutjs “Uncaught Error: Unable to parse bindings.” error, but code works
In knockoutjs “Uncaught Error: Unable to parse bindings.” error, but code works
我正在使用 knockout js 映射,在尝试执行示例时出现控制台错误。
Uncaught Error: Unable to parse bindings. Message: ReferenceError:
loadUserData is not defined; Bindings value: click: loadUserData
这里是代码,
<form action="#" method="post">
<p>First name: <input data-bind='value: firstName' /></p>
<p>Last name: <input data-bind='value: lastName' /></p>
<div>
Your favorite food:
<select data-bind='options: activities, value: favoriteHobby'></select>
</div>
<p><button data-bind='click: loadUserData'>Load data</button></p>
<p><button data-bind='click: saveUserData'>Save Data</button></p>
</form>
<script>
function PersonViewModel() {
var self = this;
self.firstName = "";
self.lastName = "";
self.activities = [];
self.favoriteHobby = "";
self.loadUserData = function () {
$.getJSON("/get-user-data.json", function (data) {
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
}
self.saveUserData = function () {
var data_to_send = { userData: ko.toJSON(self) };
$.post("/save-user-data", data_to_send, function (data) {
alert("Your data has been posted to the server!");
}).fail(function () {
alert("Ensure the Url before save the data");
});
}
}
ko.applyBindings(new PersonViewModel());
</script>
get-user-data.json
{
"firstName": "John",
"lastName": "Smith",
"activities": [ "Golf", "Kayaking", "Web Development" ],
"favoriteHobby": "Golf"
}
点击加载数据按钮时出现错误,但示例有效,如何清除此错误。
正如@user3297291 所指出的,您不能调用 ko.applyBindings 两次。相反,让你的视图模型的属性 knockout observables 和 knockout 会在它们改变时自动更新你的 UI:
function PersonViewModel() {
var self = this;
self.firstName = ko.observable();
self.lastName = ko.observable();
self.activities = ko.observableArray();
self.favoriteHobby = ko.observable();
self.loadUserData = function () {
return $.getJSON("/get-user-data.json", function (result) {
self.firstName(result.firstName)
.lastName(result.lastName)
.favoriteHobby(result.favoriteHobby)
.activites(result.activities);
});
}
}
ko.applyBindings(new PersonViewModel());
我正在使用 knockout js 映射,在尝试执行示例时出现控制台错误。
Uncaught Error: Unable to parse bindings. Message: ReferenceError: loadUserData is not defined; Bindings value: click: loadUserData
这里是代码,
<form action="#" method="post">
<p>First name: <input data-bind='value: firstName' /></p>
<p>Last name: <input data-bind='value: lastName' /></p>
<div>
Your favorite food:
<select data-bind='options: activities, value: favoriteHobby'></select>
</div>
<p><button data-bind='click: loadUserData'>Load data</button></p>
<p><button data-bind='click: saveUserData'>Save Data</button></p>
</form>
<script>
function PersonViewModel() {
var self = this;
self.firstName = "";
self.lastName = "";
self.activities = [];
self.favoriteHobby = "";
self.loadUserData = function () {
$.getJSON("/get-user-data.json", function (data) {
var viewModel = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
}
self.saveUserData = function () {
var data_to_send = { userData: ko.toJSON(self) };
$.post("/save-user-data", data_to_send, function (data) {
alert("Your data has been posted to the server!");
}).fail(function () {
alert("Ensure the Url before save the data");
});
}
}
ko.applyBindings(new PersonViewModel());
</script>
get-user-data.json
{
"firstName": "John",
"lastName": "Smith",
"activities": [ "Golf", "Kayaking", "Web Development" ],
"favoriteHobby": "Golf"
}
点击加载数据按钮时出现错误,但示例有效,如何清除此错误。
正如@user3297291 所指出的,您不能调用 ko.applyBindings 两次。相反,让你的视图模型的属性 knockout observables 和 knockout 会在它们改变时自动更新你的 UI:
function PersonViewModel() {
var self = this;
self.firstName = ko.observable();
self.lastName = ko.observable();
self.activities = ko.observableArray();
self.favoriteHobby = ko.observable();
self.loadUserData = function () {
return $.getJSON("/get-user-data.json", function (result) {
self.firstName(result.firstName)
.lastName(result.lastName)
.favoriteHobby(result.favoriteHobby)
.activites(result.activities);
});
}
}
ko.applyBindings(new PersonViewModel());