bsatrom / Knockout.Unobtrusive

Unobtrusive View to ViewModel bindings for KnockoutJS
http://bsatrom.github.com/Knockout.Unobtrusive/
44 stars 1 forks source link

can't use json syntax for custom bindings. #15

Open jwize opened 12 years ago

jwize commented 12 years ago

I tried to set up some custom bindings where elements had multiple binding and it was pretty ugly to have to concat strings together to produce binding options.

I added this code. I don't use github currently so I will take some time to read on how to fork things but I have so far updated the code to allow a nicer binding syntax.

         // This is not cleaned up and but seems to work as expected. 
         for (customKey in value) {
                    if (!__hasProp.call(value, customKey)) continue;
                    customValue = value[customKey];
                    var customParams = [];

                    for (var o in customValue) {
                        // is a string.
                        if (typeof (customValue) == "string") {
                            customParams.push(customValue);
                            break;
                        }
                        if (typeof (customValue[o]) == "string") {
                            var param4 = o + " : " + customValue[o];
                            customParams.push(param4);
                            continue;
                        }
                        // is an object.
                        if (typeof (customValue[o]) == "object") {

                            // is an array.
                            if ($.isArray(customValue[o])) {
                                for (var o2 in customValue[o]) {
                                    var innerObj = customValue[o][o2];

                                    if (typeof (innerObj) == "object") {
                                        for (var o3 in innerObj) {
                                            var param2 = o + " : { " + o3 + " : " + innerObj[o3] + " } ";
                                            customParams.push(param2);
                                        }
                                    }
                                    else {
                                        customParams.push(innerObj);
                                    }
                                }
                            } else {
                                var innerObj2 = customValue[o];
                                if (typeof (innerObj2) == "object") {
                                    for (var o4 in innerObj2) {
                                        var param3 = o + " : { " + o4 + " : " + innerObj2[o4] + " } ";
                                        customParams.push(param3);
                                    }
                                }
                                else {
                                    var param = o + " : " + customValue[o];
                                    customParams.push(param);
                                }
                            }
                        }

                    }
                    var params = customParams.join();
                    createElementBinding(customKey, params);
                }

With the use of this upgrade you can now bind your elements in a more friendly manner. where email, emailPreview and fullName represent html elements. NOTE that the expression part of the binding is wrapped with a string otherwise it would be evaluated prior to being added dynamically to the element. expressions below are as follows separated by commas. (eg "'mailto:' + SelectedCard().CardData.PrimaryEmail()", "mode() == 'edit' && isMyCard()", "mode() == 'layout'" and so on.. as in the examples below) Also note that the css in the example is a list so you can add multiple bindings to the css property; this should work fine for attr bindings as well.

    custom:
        {
            email:
                {

                    attr: { href: "'mailto:' + SelectedCard().CardData.PrimaryEmail()" },
                    visible: "mode() == 'edit' && isMyCard()"
                },
            emailPreview:
                {
                    css: [{
                        drag: "mode() == 'layout'",
                        someclass: "someclassActive()"
                    }],
                    visible: "mode() != 'edit' || !isMyCard()"
                },
            fullName:
                {
                    visible: "mode() == 'edit' && isMyCard()",
                    cardValueChanged : "SelectedCard().CardData.FullName",
                    valueUpdate : '"afterkeydown"'
                }

}