敲除控制台日志和可观察的未定义
我看过关于控制台记录可观察量的评论,但它似乎对我不起作用。另外,我的应用程序没有以我期望的默认变量启动。 这两个问题放在一起是因为我怀疑它们以某种方式相关。
HTML
<select data-bind="options: widgets, optionsText: 'name', value: current_widget, optionsCaption: 'Choose...'"></select>
<input data-bind="value: current_widget() ? current_widget().name : 'nothing'" />
Javascript
var cdta = {};
$(document).ready(function(){
cdta.widgets_data = [{ "name": "foo", "id": "1"},{ "name": "bar", "id": "2"},{ "name": "bash", "id": "3"},{ "name": "baq","id": "4"}];
cdta.local = {};
cdta.local.current_widget = { "name": "foo", "id": "1"};
alert('current_widget starting value should be: ' + cdta.local.current_widget.name);
function app() {
var self = this;
//widgets
self.widgets = ko.observableArray(cdta.widgets_data);
// which widget to display from a local source
alert('about to asign a current_widget named:' + cdta.local.current_widget.name);
self.current_widget = ko.observable(cdta.local.current_widget);
}
ko.applyBindings(new app());
alert('after applying bindings name of current widget: ' + current_widget().name);
//expecting foo
//but doesn’t alert because current_widget isnt defined
});
I've seen comments on console logging observables but it doesn’t seem to work for me. Additionally my app isn't starting up with a the default var that I was expecting.
The two questions are together because I suspect they're related somehow.
HTML
<select data-bind="options: widgets, optionsText: 'name', value: current_widget, optionsCaption: 'Choose...'"></select>
<input data-bind="value: current_widget() ? current_widget().name : 'nothing'" />
Javascript
var cdta = {};
$(document).ready(function(){
cdta.widgets_data = [{ "name": "foo", "id": "1"},{ "name": "bar", "id": "2"},{ "name": "bash", "id": "3"},{ "name": "baq","id": "4"}];
cdta.local = {};
cdta.local.current_widget = { "name": "foo", "id": "1"};
alert('current_widget starting value should be: ' + cdta.local.current_widget.name);
function app() {
var self = this;
//widgets
self.widgets = ko.observableArray(cdta.widgets_data);
// which widget to display from a local source
alert('about to asign a current_widget named:' + cdta.local.current_widget.name);
self.current_widget = ko.observable(cdta.local.current_widget);
}
ko.applyBindings(new app());
alert('after applying bindings name of current widget: ' + current_widget().name);
//expecting foo
//but doesn’t alert because current_widget isnt defined
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中有几个问题。
current_widget是应用程序的一个属性,所以你需要做这样的事情
由于您使用的是 value 和 optionsCaption 出价,默认情况下,knockout 会将绑定到 value 的 observable 设置为未定义。如果您删除 optionsCaption 绑定,它将起作用。如果您需要 optionsCaption 并且需要选择初始值,则必须在应用绑定后重置它:
更新:
我在#2 上错了。应用绑定后不必重置值。真正的问题是您使用完全不相关的对象(不是来自数组)作为初始值。这将解决这个问题:
There are couple of issues in your code.
current_widget is a property of app, so you need to do something like this
Since you are using value and optionsCaption bidnings, knockout will set the observable that's bound to value to undefined by default. If you remove optionsCaption binding it will work. If you need optionsCaption and you need to select initial value, you will have to reset it after applying bindings:
UPDATE:
I was wrong on #2. You don't have to reset value after applying bindings. The real problem is that you use completely unrelated object (not from the array) for initial value. This will fix the issue:
但变量/方法 current_widget 确实未定义。
KnockoutJS
不会生成全局变量。如果您想访问 viewModel 之外的 viewModel 数据,那么您需要将其存储在某个地方(变量、窗口等)。
But variable/method
current_widget
indeed undefined.KnockoutJS
doesn't generate gloabal variables.If You want to access viewModel data outside of viewModel, then You need to store it somewhere(variable, window, etc).