Namespacing custom binders

36 views
Skip to first unread message

imm102

unread,
Jan 5, 2011, 12:15:47 PM1/5/11
to KnockoutJS
Hi,

When building reusable components like the simple grid
http://knockoutjs.com/examples/resources/knockout.simpleGrid.js is it
possible to namespace the custom binding handlers in some way.

I tried something like this

ko.bindingHandlers.tree = { }
ko.bindingHandlers.tree.rename = { }

but that didn't work. I'm thinking in one 'plugin' you may create
multiple custom bindings that potentially could have the same names as
other custom binding and they would break each other.

Is there an alternative way to package these functionality sets?

Cheers,

Ian

fla...@gmail.com

unread,
Jan 8, 2011, 1:05:17 PM1/8/11
to KnockoutJS
Hi Ian

Yes, if you want you can put dots in the name of a custom binding. The
trick to make it work is to note that data-bind syntax *is* JSON, so
if you are going to use characters that you can't use in a JS variable
name, you need to put it in quotes. For example,

ko.bindingHandlers["imm102.tree.rename"] = {
....
}

and then

<div data-bind="'imm102.tree.rename':blah">...</div> (notice the
single quotes inside the double ones)

Alternatively, if you don't like the quotes, then use another
character like underscore to demark the segments of your binding name,
e.g.: imm102_tree_rename

To be honest I wouldn't worry about namespacing all that much right
now: jQuery doesn't advise people to use namespaced names for custom
plugins, there are bazillions of jQuery plugins being shared, and
people don't generally complain about name clashes. However if you
want to then go ahead :)

Steve


On Jan 5, 5:15 pm, imm102 <ianmmc...@gmail.com> wrote:
> Hi,
>
> When building reusable components like the simple gridhttp://knockoutjs.com/examples/resources/knockout.simpleGrid.jsis it
Reply all
Reply to author
Forward
0 new messages