Routing function with prefixing - #15
Conversation
|
@mberkom - still intend on looking through this soon. Thanks for submitting it. |
|
Sounds good. I just squashed all the commits together to make things easier to follow. |
|
@mberkom Hi Michael- I took a look at this a bit last weekend. Pretty interesting. I think that I would probably use the I think though to keep this library a bit simpler for the time being, I would like to hold off on merging this in, although I certainly see the benefits of this approach. |
|
@rniemeyer Sounds good. I'm going to continue to use it in the project I'm working on. So far, the simple data attribute mechanism is working fine and doesn't annoy me much. It has the added benefit of being immediately obvious when you open up the inspector... However, I could see switching to |
|
@rniemeyer Would it be possible to reconsider what it'd take to get this feature included officially? I've found it to be incredibly handy and easy to use. However, there are a couple of areas that could certainly use more thought.
For most Knockout developers doing simple projects, this library is probably overkill. On the other hand, those of us doing complex projects need every last bit of organization & re-usability that we can get. |
Prefixing is disabled by default and can be enabled via the options hash. If enabled, it will only decorate nodes that have a `data-class` attribute with a `data-class-prefix` attribute. Otherwise, it uses `ko.utils.domData` to keep track of things.
|
@mberkom - I am definitely willing to consider putting this type of functionality into the library. I do think that I will have to take another look through the code. It would be nice if we didn't need to have |
|
@rniemeyer Cool! Make sure to look at my latest commit description. It explains what I did. Yeah, I'm not a fan of having |
The getPrefix method wasn't finding the parentElement property on the virtual element node and so wasn't traversing up the DOM tree to find the correct binding prefix.
I extended the
getBindingsmethod to call agetCleanPrefixfunction that recurses up the DOM tree looking for nodes with adata-class-prefixattribute and returning the complete prefix once it hits the<body>tag or an element with adata-class-prefixcontaining "$root".The newly generated class prefix is then passed to the binding router and prepended to the class name before it attempts to look up the binding in the bindings object.
This allows you to write HTML like this:
instead of...
Technical Details
The
getPrefixmethod decorates all child nodes of an element with adata-class-prefixattribute containing "$root". This is easiest to understand if you open up your inspector and look at the generated HTML. It adds thedata-class-prefixattribute to all nodes, so that the second time a child element on the same branch goes to get its prefix, it doesn't have to recurse all the way up the tree again.Knockout's
ko.domDatamight be an effective way to clean things up, but I'm not sure it's the most straightforward thing to implement and might be a source of memory leaks if DOM elements are removed outside of Knockout.Thoughts?