Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

observableArray accepting non-array value #733

Closed
fastfasterfastest opened this issue Nov 24, 2012 · 4 comments
Closed

observableArray accepting non-array value #733

fastfasterfastest opened this issue Nov 24, 2012 · 4 comments
Milestone

Comments

@fastfasterfastest
Copy link

[Pardon me, I just commented on an old closed issue, https://github.com//issues/63, and I don't know if that will bring it to the forefront, so thus I am creating a new issue...]

Currently, ko.observableArray accepts non-array values and one ends up with an observable array whose current value isn't even an array - that happens when passing null or undefined to ko.observableArray.

One could argue that ko.observableArray should either not allow null or undefined to be passed, or, if allowed, that the behavior should be the same as if no parameters were passed, and thus result in an underlying empty array.

E.g., one can currently ko.observableArray().removeAll() without any errors, while ko.observableArray(undefined).removeAll() results in an error during the removeAll call. Once an observableArray has been initialized with ko.observableArray(undefined), you are currently on thin ice.

@SteveSanderson
Copy link
Contributor

I agree we should consider treating ko.observableArray(undefined) and ko.observableArray(null) the same as ko.observableArray(). Do you want to submit a pull request?

@fastfasterfastest
Copy link
Author

I don't do github yet... but I would change ko.observableArray:

--- From ---

ko.observableArray = function (initialValues) {
    if (arguments.length == 0) {
        // Zero-parameter constructor initializes to empty array
        initialValues = [];
    }
    if ((initialValues !== null) && (initialValues !== undefined) && !('length' in initialValues))
        throw new Error("The argument passed when initializing an observable array must be an array, or null, or undefined.");

    var result = ko.observable(initialValues);
    ko.utils.extend(result, ko.observableArray['fn']);
    return result;
}

--- To ---

ko.observableArray = function (initialValues) {
    if ((initialValues === undefined) || (initialValues === null)){
        // Passing no value, undefined or null initializes to empty array
        initialValues = [];
    }
    if (!('length' in initialValues))
        throw new Error("The argument passed when initializing an observable array must be an array, or null, or undefined.");

    var result = ko.observable(initialValues);
    ko.utils.extend(result, ko.observableArray['fn']);
    return result;
}

@SteveSanderson
Copy link
Contributor

Thanks! I'll leave the issue open until we get chance to implement a fix.

@paglias paglias mentioned this issue Mar 6, 2013
22 tasks
rniemeyer added a commit that referenced this issue Mar 16, 2013
rniemeyer pushed a commit that referenced this issue Mar 17, 2013
rniemeyer added a commit that referenced this issue Mar 17, 2013
mikekidder pushed a commit to mikekidder/knockout that referenced this issue Apr 17, 2013
mikekidder pushed a commit to mikekidder/knockout that referenced this issue Apr 17, 2013
@milaan-muc
Copy link

Is it possible to modify the behavior of the "if" binding to check if the variable provided is actually an observablearray and if yes, it checks the length and returns true/false.

I think its very ugly to always write "ko if: obArray().length" in html templates and "ko if: obArray" would make everything less complex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants