Appropriate use of metadata in polymer application

2019-08-03 04:03发布

I'm confused as to the proper use of core-meta in polymer. I tried looking at Topeka's metadata.html but it seems to cloud the issue for me further, with its extensive usage of x-meta tags seemingly related specifically to polymer designer tool etc. (I'm not using polymer designer)

Basically, I'm simply looking to create a config-file of sorts for my polymer application. For instance, I'd like to set the application's API data source URL (or at least the base URL) in one place and have any of the various elements reference it as required. Would that approach be considered best practice in polymer applications? In other words, should one use a separate metadata.html containing core-meta tags to accomplish the aforementioned goal for an application? If so, I'd like to see a brief example if possible. Thanks!

1条回答
别忘想泡老子
2楼-- · 2019-08-03 04:19

core-meta is a generic element you can use for sharing information across the DOM tree. It uses monostate pattern such that any instance of core-meta has access to the shared information. You can use core-meta to share whatever you want (or create an extension [like x-meta] for enhancements).

The core-meta instances containing your actual data can be loaded in an import, or constructed in any way you see fit. The only requirement is that you create them before you try to access them.

Examples:

If I create an instance like this:

<core-meta id="info" keyUrl="foo/bar"></core-meta>

Note that keyUrl="foo/bar"is the metadata I've defined. I could define more attributes or use child nodes to define additional metadata.

Now I can access that element (and it's metadata) from any core-meta instance via the myId method, e.g.

meta.byId('info').getAttribute('keyUrl').

Pure imperative form would be like:

document.createElement('core-meta').byId('info').getAttribute('keyUrl');

Or, in a Polymer element, you can include a meta in your template:

<core-meta id="meta"></core-meta>
...
this.$.meta.byId('info').getAttribute('keyUrl');

You could sugar the byId('info').getAttribute('keyUrl') expression any number of ways to make access more convenient depending on your use case.

Live code: http://jsbin.com/vipoyi/1/edit

查看更多
登录 后发表回答