-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Comments
I agree we should consider treating |
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;
} |
Thanks! I'll leave the issue open until we get chance to implement a fix. |
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. |
[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.
The text was updated successfully, but these errors were encountered: