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

allow binding handlers to pre-process bindings #18

Open
mbest opened this issue Mar 28, 2012 · 7 comments
Open

allow binding handlers to pre-process bindings #18

mbest opened this issue Mar 28, 2012 · 7 comments

Comments

@mbest
Copy link
Owner

mbest commented Mar 28, 2012

insertPropertyAccessors already does some pre-processing, but it may make sense to move this to the binding handlers. They would have a function that takes a value string and returns a new value string.

I was thinking of this in the context of SteveSanderson/knockout/knockout#388 where it would be cool to be able to have something like foreach: $wizardStep in steps that would be translated to foreach: {data: steps, itemName: '$wizardStep'}.

@mbest
Copy link
Owner Author

mbest commented Mar 28, 2012

It might be useful to have the pre-processor also be able to change the binding key (or have a different pre-processor function). For example, with: $wizard = myWizard could be changed to template: {if: myWizard, data: myWizard, dataName: '$wizard'}

@vamp
Copy link

vamp commented Mar 29, 2012

if 'click' binding added later (e.g. using ko.conventions), preprocess not working at all

@mbest
Copy link
Owner Author

mbest commented Mar 29, 2012

Good catch. I hadn't thought of that. Where can I find ko.conventions?

@vamp
Copy link

vamp commented Mar 30, 2012

check this: http://blog.stevensanderson.com/2011/12/21/knockout-2-0-0-released/
section: 5. Binding providers (and hence external bindings)
or fiddle: http://jsfiddle.net/StevenSanderson/2eY2u/

@mbest
Copy link
Owner Author

mbest commented Mar 30, 2012

Thanks.

@mbest
Copy link
Owner Author

mbest commented Apr 4, 2012

My current idea is that the preprocess function can return either a string, to replace the value of the current binding, or return an array, to replace or add to the current binding. The array will contain one or more key/value pairs as two-element arrays:

[
    [ 'key1', 'value1' ],
    [ 'key2', 'value2' ]
]

The function is passed two parameters: value and key, both strings.

@mbest
Copy link
Owner Author

mbest commented Apr 4, 2012

One issue to address is that new bindings that are added by preprocess need to be at the root level but won't be currently if preprocess is called from the second level of a two-level binding.

I also had another idea for the preprocess interface. It could return either a string value, to replace the current binding, or undefined (a falsy value), to skip the current binding. A third parameter would be a callback function to add new bindings:

preprocess: function(value, key, addBinding) {
    ...
    addBinding('key1', 'value1');
    addBinding('key2', 'value2');
}

mbest added a commit that referenced this issue Apr 4, 2012
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

2 participants