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 should accept also single paramters or a parameter list instead of only arrays #63
Comments
Thanks very much for the suggestion! I agree that it would be useful to accept zero parameters and automatically initialize to an empty observableArray (e.g., var x = ko.observableArray()), which was not previously the case, so I've just implemented that. However I don't think it should automatically coerce other passed values to arrays, as it would then get very confusing if for example you wanted to initialize it to "null" or "undefined" (because then you'd end up with an array containing a single value that was null or undefined, which you'd almost never want). Similarly there are odd cases like if you passed a string (e.g., ko.observable("some string")), you might expect it to be coerced into an array containing that string, but in fact strings are special cases of arrays (they are arrays of characters) so it would not in fact be coerced at all. In the end there'd be a big bunch of special cases to try to interpret people's likely meanings. It's much tidier to interpret any passed value as-is. However, I agree it's confusing if you pass a non-array value and end up with an observable array whose current value isn't even an array, so I've added a check so the constructor will throw an error if you try to initialize to any value that isn't array-like, or null, or undefined. |
Thanks too. That is also a good solution. |
I agree that it would be confusing to pass a non-array value and end up with an observable array whose current value isn't even an array. But, currently ko.observableArray allows exactly that - when passing null or undefined. 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(), while ko.observableArray(undefined).removeAll() errors in the removeAll call. Once an observableArray has been initialized with ko.observableArray(undefined), you are currently on thin ice. |
See titel.
The text was updated successfully, but these errors were encountered: