Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@lelandrichardson
Copy link

added a simple check for value being an array, and converted to a
space-separated string if true.

I believe this to be the expected behavior - since currently it ends up
serializing the array indices to a string which doesn't even result in
valid CSS classes.

added a simple check for value being an array, and converted to a
space-separated string if true.

I believe this to be the expected behavior - since currently it ends up
serializing the array indices to a string which doesn't even result in
valid CSS classes.
@vamp
Copy link

vamp commented May 3, 2013

but - if array is collection of observables?

@lelandrichardson
Copy link
Author

@vamp fair enough. I wouldn't want to add too much overhead to this method, so I'm trying to think about the ideal way to test for this.

One could use:

        if(Object.prototype.toString.call(value) === '[object Array]' && value.length > 0){
            if(ko.isObservable(value[0])){
                value = ko.toJS(value);
            }
            value = value.join(' '); // .join() will call .toString() on entities
        }

Are there any other cases we should consider?

@vamp
Copy link

vamp commented May 7, 2013

Maybe you want to solve your situation using css binding customization (in your own project without pull request)?

ko.bindingHandlers.css.update = (function(underlying){
    return function(element, valueAccessor){
        return underlying(element, function(){
            var value = ko.utils.unwrapObservable(valueAccessor());
            return (value instanceof Array)
                ? ko.toJS(value).join(' ')
                : value;
        });
    };
})(ko.bindingHandlers.css.update);

@lelandrichardson
Copy link
Author

@vamp yes, clearly I could make my own solution fairly easily (which I have), however, this is a particular issue which I felt should actually be the default behavior.

As it stands, passing an array of strings into this binding (which seems like a fairly reasonable thing to do since the class attribute is essentially treated as just a space-separated list of strings) will result in not only a meaningless result, but invalid HTML.

Thanks for the code snippet though - I do like how you have done it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants