Ideas of metadata

Ideas of metadata

jquery.metadata.js

I downloaded jquery-ui-1.8rc1 pack and found a jquery.metadata.js plugin in external directory, it seems to had been used for widgets pre-configuration since 2006... but just exposed as a stand-alone plugin in the recent version.

Because I have used a very simular jquery.meta.js for a long time, and I think this feature is totally OK to be included in jquery core, I open this idea topic to look for a discussion:



meta & meta in html

In my opinion, a meta of something is a data that is:
  1. Attributes that is presetted, but not generated in the runtime.
  2. Written in static plaintext, but not a functional value-builder something.
  3. Readonly.
HTML has it's own "meta data of document": the <meta> elements. A easy extension of this is the idea of "meta data of elements": since we all madly append kinds of extra data on elements, why not build a preset mechanism?

implemention

I used a nonstandard html attribute named 'meta' and a JSON type value to record my metadata:

<div meta="{ widgetOpts: {  silder: { min: 10, max: 90 }  }}">...<div>

jquery.metadata.js in jquery-ui-1.8rc1 used the same JSON idea but supports two other formats:
  1. in class attribute:
    <div class="blahblah { silderOpts: { min: 10, max: 90 } }">...</div>
  2. in a child element:
    <div><script>{ silderOpts: { min: 10, max: 90 } }</script></div>
Other differents includes: its default format is class, and its default attribute name is "metadata", and these are all configurable.






discussion

I'm... not very agree to the class attribute format, in fact. An extra attribute or an extra child element is not standard, but still legal for HTML syntax, but JSON in class is not. If the jquery lib just used this mechanism as a temporary attribute in initializing phase, and remove it after that, it's OK, just not very friendly to other libs which may check attribute values; but if we want this feature to be public, I think attribute format will be a better choice.


selector

Meta is the presetted, readonly, static, plaintext attribute -- and still, it's attribute, just a little more complex. So we might want to use them as selector like other real attributes:

I give an implemention in my plugin to support selectors like these:
  • div[meta]                 matches <div> elements which have meta attribute
  • [meta.widgetOpts]    matches metas like meta="{ widgetOpts: { ... }, ... }"
  • [meta.order=4]         matches metas like meta="{ order=4, ... }"
They're very simular like traditional attribute selector (in fact, the first one IS the traditional selector), and use dot to match deep structure in JSON object.

A known problem of this implemetion is speed: I must eval JSON every time in selector processing to get the js object. Cache the eval result in .data() might be a solution... but I got a little worry about name confliction.







auto-init

This is another worry about jquery.metadata.js. In jquery.metadata.js, value of metadata will be auto appended to init parameters of widgets, which means users are not allowed to put them own data in metadata, and two widgets can't have each different metadata in one element.

This might be a common fault of all plugins with auto-init mechanism. I have considered a plugin to manage all auto-init processes, and found it's just same as a document.ready. A convention of meta structure -- like "{ widgetOpts: { widgetname: { ... }}}" -- is my answer to this question, but it also has obvious weakness: too complex of simple parameters. Expecting better solution.




others

I included two util functions in my plugin to get <meta> elements values. Just a shortcut.




That's all. I uploaded a full demo of my jquery.meta.js and the jquery.metadata.js in jquery-ui-1.8rc1 as attachments. Waiting for you replys. Thanks.