Created by: thomastuts
I've implemented namespacing since I needed it for my current project, and because there was an issue for it already anyway (see #2 (closed)). I've added a couple of tests that all pass. It supports the following functionality:
- Adding a namespace
- Adding an optional namespace delimiter (defaults to
.
) - Specifying either a custom namespace or no namespace on a per-method basis (i.e.
get()
,set()
andremove()
). If the namespace isnull
, it will not use any namespace to get/set/remove that value (similar to the current implementation). If the namespace is given and not null, it will use that namespace to perform the operation. I'm not sure about the syntax yet, since I didn't want to change too much of the original API.
Also: you might notice that I have replaced all references to this
with a top-level store
object, this way we can reference it in the helper function that creates the namespaced key.
Namespacing configuration
I didn't want to create a whole new provider just to be able to configure things in Angular's config()
block so I've declared the configuration on the service instead. There are two namespacing options you can set:
-
store.namespace
which defaults tonull
(current behaviour) -
store.namespaceDelimiter
which defaults to.
- this will result in stored keys such asnamespace.key
Custom/no namespacing syntax
In the store
methods, the namespace parameter can either be omitted (default behaviour), null
where it will not use any namespace, or a value which will be used for the namespace instead of the default one.
store.get(name, namespace)
store.remove(name, namespace)
-
store.set(name, elem, namespace)
-- This is the syntax I'm not sure about since the other two methods have the custom namespace right after the key name. I think this way is better though, since this behaviour most likely won't be used a lot, and if we moved it to the second argument, people would have to typestore.set('someName', null, 'someValue')
all the time. Would love to hear your feedback on this.