4

I'm having an issue using a 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. 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. Any help here would be greatly appreciated.

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

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;
};
1
  • 1
    Updated to be less verbose. Commented Jun 3, 2014 at 17:33

1 Answer 1

3

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 i.e. self is the container but slides() has not created. It's a circle reference. So container.slides() is undefined.

try something like this container.slides() && self.position() === container.slides().length - 1

Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. Thanks. I opted to create an empty slides array and then push new Slide instances onto it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.