简体   繁体   English

Knockout通过访问observableArray来计算observable

[英]Knockout computed observable with access to observableArray

I'm having an issue using a computedObservable . 我在使用computedObservable遇到了问题。 They seem pretty straight forward, but I think my use case might be a little odd. 他们看起来很直接,但我认为我的用例可能有点奇怪。 The issue is when onBottom() is called, it doesn't see the items that are in the container.slides() array. 问题是当onBottom()时,它不会看到container.slides()数组中的项。 Instead, I get "Undefined is not a function". 相反,我得到“未定义不是一个功能”。

Since onTop() works fine, it makes me think that it's the relationship between Slide and FeaturedContent that is causing the issue, but I think it should work fine. 由于onTop()工作正常,它让我认为它是导致问题的SlideFeaturedContent之间的关系,但我认为它应该工作正常。 Any help here would be greatly appreciated. 这里的任何帮助将不胜感激。

HTML HTML

        <div class="section-block" data-bind="foreach: slides">
            <div class="row well">
                <div class='control'>
                    <a href='#' data-bind="click: moveUp, ifnot: onTop">
                        <i class="fa fa-arrow-up"></i>
                    </a>
                    <a href='#' data-bind="click: moveDown, ifnot: onBottom">
                        <i class="fa fa-arrow-down"></i>
                    </a>
                    <span class='pull-right'>Remove</span>
                </div>
                <h5 data-bind="text: headline"></h5>
                <p data-bind="text: image"></p>
            </div>
        </div>

JS JS

var FeaturedContent = function() {
    var self = this;

    self.maxSlides = 14;
    self.slides = ko.observableArray([
        new Slide({}, "I should be last", "someimg", 0, self),
        new Slide({}, "I should be first", "anotherimg", 1, self),
        new Slide({}, "I should be second-", "anotherimg", 2, self),
    ]);

    // ... snipped.

};

var Slide = function(contentItem, headline, image, order, container) {
    var self = this;

    // -- Data
    self.contentItem = contentItem;
    self.headline = headline;
    self.image = image;
    self.position = ko.observable(0);

    self.onBottom = ko.computed(function() {
        return (self.position() === container.slides().length - 1) ? true : false;
    });

    self.onTop = ko.computed(function() {
        return (self.position() === 0) ? true : false;
    });

    return self;
};

During creating 在创建过程中

self.slides = ko.observableArray([
    new Slide({}, "I should be last", "someimg", 0, self),
    new Slide({}, "I should be first", "anotherimg", 1, self),
    new Slide({}, "I should be second-", "anotherimg", 2, self),
]);

in the FeaturedContent your code will create new Slide and use self to get container.slides().length ie self is the container but slides() has not created. FeaturedContent您的代码将创建new Slide并使用self来获取container.slides().lengthselfcontainerslides()尚未创建。 It's a circle reference. 这是一个圆圈参考。 So container.slides() is undefined. 所以container.slides()是未定义的。

try something like this container.slides() && self.position() === container.slides().length - 1 尝试像这样的container.slides() && self.position() === container.slides().length - 1

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

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