简体   繁体   English

在可观察数组内访问可观察对象的属性-kickout.js

[英]Accessing property of observable object inside observable array - knockout.js

Been learning knockout and having fun, but I'm stuck on something that I assume is so simple: accessing the properties of an observable object inside an observable array. 我一直在学习淘汰赛并从中获得乐趣,但是我坚持做一个简单的事情:访问可观察数组中可观察对象的属性。 I need to get the "multiplierValue" from the activeCompMultiplier object inside the activeCompMultipliers array. 我需要从activeCompMultipliers数组内的activeCompMultiplier对象获取“ multiplierValue”。

Here's all my relevant code: 这是我所有相关的代码:

First off, the object that is created and pushed into the array. 首先,创建并推入数组的对象。 This is what I'm trying to talk to: 这是我要与之交谈的内容:

var activeCompMultiplier = function (multiplierValue) {
    this.multiplierValue = ko.observable(multiplierValue);
}

And this is the observable array that contains the observable objects: 这是包含可观察对象的可观察数组:

self.activeCompMultipliers = ko.observableArray();

Here's the line that creates a new object and pushes it into the array: 这是创建新对象并将其推入数组的行:

self.activeCompMultipliers.push(new activeCompMultiplier(1));

And finally, when the input field triggers a 'change' event, this is what is called. 最后,当输入字段触发“更改”事件时,这就是所谓的。 I'm using an alert() to debug the issue: 我正在使用alert()调试问题:

self.inputChanged = function (rowIndex) {

    alert("Multiplier is: " + self.activeCompMultipliers()[rowIndex].multiplierValue() + "\n At index: " + rowIndex);
}

Annnnd here my related HTML: 这里是我的相关HTML:

<tbody data-bind="foreach: activeParts">
                <tr>
                    <td class="ShoppingEntry" data-bind="text: $data"></td>
                    <td class="Textbox" >
                        <input type="number" class="TextBoxInput"
                          data-bind="value: $root.activeCompMultipliers()[$index()], 
                                     event: { change: inputChanged.bind(self, $index())},
                                     attr: {id: 'Textbox' + $index()}" /></td>     
                    <td class="DeleteCell">
                        <input type="image" src="/Assets/list_remove.png" class="DeleteButtonInput" data-bind="click: deactivatePart"/></td>
                </tr>
</tbody>

I assume its a syntactical issue as JS isn't my strongest language, but I've triple-checked using parenthesis to un-wrap KO observables, so maybe it's a scope issue? 我认为这是一个语法问题,因为JS不是我最强的语言,但是我已经通过括号进行了三次检查,以解开KO可观察值的包装,所以这可能是范围问题?

All of my JS is within a function ViewModel() {...} function, that is then bound via ko.applyBindings(new ViewModel()); 我所有的JS都在一个function ViewModel() {...}函数中,然后通过ko.applyBindings(new ViewModel());进行绑定ko.applyBindings(new ViewModel());

As a long-time lurker, I appreciate any help. 作为长期潜伏者,我非常感谢您的帮助。 :) :)

3 points: 3分:

  1. You're not looping over activeCompMultipliers but using the $index() of activeParts to access it, does this mean these 2 arrays have the same size and are linked? 您不是在遍历activeCompMultipliers而是使用activeParts$index()访问它,这是否意味着这两个数组具有相同的大小并已链接?

  2. If the index() is correct, the value is bound to an activeCompMultiplier instance, it should be bound to its multiplierValue field: 如果index()是正确的,则将值绑定到activeCompMultiplier实例,则应将其绑定到其multiplierValue字段:

     <input type="number" class="TextBoxInput" data-bind="value: $root.activeCompMultipliers()[$index()].multiplierValue, 
  3. The change event is bound to inputChanged which you changed its scope to self , but what is self at that point? change事件绑定到inputChanged ,您已将其范围更改为self ,但是那一刻self是什么? and its useless as that function already references the right self . 它无用,因为该函数已经引用了正确的self Should be: 应该:

     event: { change: function() { inputChanged($index()); } } 

    or at least: 或至少:

     event: { change: inputChanged.bind(null, $index()) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM