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

Support parameterless binding syntax #289

Closed
jonrimmer opened this issue Jan 23, 2012 · 9 comments
Closed

Support parameterless binding syntax #289

jonrimmer opened this issue Jan 23, 2012 · 9 comments

Comments

@jonrimmer
Copy link

Custom bindings are often used to attach scripted behaviours to elements, particularly those created by templates. When used like this, there may be no need to supply parameters such as an observable to the binding. In these cases, it is usual to pass a placeholder parameter such as an empty string or a boolean to the binding:

<div data-bind="myImageGallery: true, visible: isGalleryVisible">
   ...
</div>

However, this is a little untidy, and it makes the code a little less comprehensible, because it is not obvious looking at the code that the parameter is not being used. It would be nice to be able to specify bindings without any parameters:

<div data-bind="myImageGallery, visible: isGalleryVisible">
   ...
</div>
@SteveSanderson
Copy link
Contributor

Thanks for suggesting this Jon.

Right now I'm not 100% sure either way about this. Currently, bindings are always parseable as a JavaScript object literal, so having exceptions to that constraint loses a bit of the elegance, and could act as a breaking change for some kinds of extensiblity (e.g., custom binding providers). I'll leave the request open and marked as "Future" for now, and we can see if other community members have an opinion.

@mbest
Copy link
Member

mbest commented Jan 23, 2012

I like the idea. Already, Knockout modifies the binding string before it's converted to a Javascript object, adding quotes to the binding names, and adding writer functions. So it wouldn't be a big stretch for it to make other modifications.

@mbest
Copy link
Member

mbest commented May 10, 2012

I'm labeling this for inclusion in v2.2 with minor impact, based on changes required for #321.

@SteveSanderson
Copy link
Contributor

Thanks for the suggestion. Personally I don't think, however, that it's going to be beneficial in the long run. Any future tooling would no longer be able to parse the bindings as JavaScript, plus if we move away from the standard constraints of JavaScript object literals, it will become very hard to decide which kinds of syntactical constructions should be allowed and which should not.

Allowing this would be similar to having CSS properties with no value, e g.:

.mySelector { color: red; hidden; } /* instead of display: none */

... or JavaScript object literals with keys implicitly mapping to undefined:

var x = { someProp: 123; anotherProp; }

These sorts of things aren't permitted, even though in some cases it would lead to less typing, as there are significant benefits in having only one pattern that rule formatting should follow. It's easier to learn and makes tooling easier to develop.

Since it's not causing any significant disadvantage to developers, I would suggest closing this proposal.

@rniemeyer
Copy link
Member

My feeling is that the binding strings should be valid JavaScript object literals.

@mbest
Copy link
Member

mbest commented May 22, 2012

My feeling is that the binding strings should be valid JavaScript object literals.

As I mentioned in #233, we already allow binding strings that aren't valid Javascript (at least in some browsers) by adding quotes around the root-level keys before eval-ing.

@rniemeyer
Copy link
Member

As I mentioned in #233, we already allow binding strings that aren't valid Javascript (at least in some browsers) by adding quotes around the root-level keys before eval-ing.

Seems a little different to me, as it is just certain browsers that have a problem with the keywords and people are usually attempting to write valid JavaScript in this case. Not a big deal though...

@mbest
Copy link
Member

mbest commented May 22, 2012

I've changed this to "future" instead of "v2.2". We can pick up this discussion again later.

@mbest
Copy link
Member

mbest commented Jun 25, 2013

This is now available in 3.0 (currently in development). If you specify a binding without a parameter, the binding is called with a parameter of undefined. Using the new preprocess functionality, any default value can be specified. Here is an example: http://jsfiddle.net/mbest/BcUN4/

@mbest mbest closed this as completed Jun 25, 2013
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

4 participants