敲除控制台日志和可观察的未定义

发布于 2025-01-03 23:42:34 字数 1430 浏览 1 评论 0原文

我看过关于控制台记录可观察量的评论,但它似乎对我不起作用。另外,我的应用程序没有以我期望的默认变量启动。 这两个问题放在一起是因为我怀疑它们以某种方式相关。

这是小提琴

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.

Here is the fiddle.

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 技术交流群。

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

发布评论

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

评论(2

绮筵 2025-01-10 23:42:34

您的代码中有几个问题。

  1. current_widget是应用程序的一个属性,所以你需要做这样的事情

    var app = new app();
    ko.applyBindings(应用程序);
    Alert('应用当前小部件的绑定名称后:' + app.current_widget().name);
    
  2. 由于您使用的是 value 和 optionsCaption 出价,默认情况下,knockout 会将绑定到 value 的 observable 设置为未定义。如果您删除 optionsCaption 绑定,它将起作用。如果您需要 optionsCaption 并且需要选择初始值,则必须在应用绑定后重置它:

    var app = new app();
    ko.applyBindings(应用程序);
    app.current_widget(ctta.widgets_data[0]); //你必须从数组中选择一个对象,而不是一个完全不相关的对象 cta.local.current_widget
    Alert('应用当前小部件的绑定名称后:' + app.current_widget().name);
    

更新:
我在#2 上错了。应用绑定后不必重置值。真正的问题是您使用完全不相关的对象(不是来自数组)作为初始值。这将解决这个问题:

cdta.local.current_widget = cdta.widgets_data[0];

There are couple of issues in your code.

  1. current_widget is a property of app, so you need to do something like this

    var app = new app();
    ko.applyBindings(app);
    alert('after applying bindings name of current widget: ' + app.current_widget().name);
    
  2. 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:

    var app = new app();
    ko.applyBindings(app);
    app.current_widget(cdta.widgets_data[0]); //you have to select an object from the array, not a completely unrelated object cdta.local.current_widget
    alert('after applying bindings name of current widget: ' + app.current_widget().name);
    

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:

cdta.local.current_widget = cdta.widgets_data[0];
陪我终i 2025-01-10 23:42:34

但变量/方法 current_widget 确实未定义。 KnockoutJS 不会生成全局变量。

如果您想访问 viewModel 之外的 viewModel 数据,那么您需要将其存储在某个地方(变量、窗口等)。

var cdta = {};
$(document).ready(function(){    
    //...    
    function app() {
       //...
    }
    window.app = new app();
    ko.applyBindings(window.app);

    alert('after applying bindings name of current widget: ' 
      + window.app.current_widget().name);
    //expecting foo
});

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).

var cdta = {};
$(document).ready(function(){    
    //...    
    function app() {
       //...
    }
    window.app = new app();
    ko.applyBindings(window.app);

    alert('after applying bindings name of current widget: ' 
      + window.app.current_widget().name);
    //expecting foo
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文