JavaScript Polymer- use behavior to share object between elements?

Is it possible to use behaviors to share a object between elements?

<script>
  selectedBehavior = {
    properties: {
      selected: Object
    }
  }
</script>

<dom-module id="paper-menu-custom">
  <style>
  </style>
  <template>
    <paper-menu attr-for-selected="name" selected="{{selected.choice}}">
    ...
  <script>
    Polymer({
        is: "paper-menu-custom",
        behaviors: [selectedBehavior]
    });

  toolbars = document.querySelector('paper-menu-custom');
  toolbars.selected.choice = "home";

Uncaught TypeError: Cannot set property 'choice' of undefined

Answer:1

You do not need to use a behavior to share information between elements.

You should use IronMeta like so :

  • Declaratively and with data-binding :

<iron-meta key="my-unique-key" value="{{mySharedInformation}}"></iron-meta>

Then use mySharedInformation the same way you would any custom element's properties. Setting it will update the value of any other <iron-meta> in your code that shares the same key.

  • In plain javascript :

Read

var mySharedInformation = new Polymer.IronMeta().byKey('my-unique-key');

Write

new Polymer.IronMeta({key: 'my-unique-key', value: mySharedInformation});

Answer:2

Take a look at my object in github (https://github.com/akc42/akc-meta), it allows one element to publish a value with a key, and other ti have multiple instances subscribe to it and get the data out again.

It does it by keeping instances in a private variable

(function(){
var private;

Polymer({element definition has access to private});


})();
Answer:3

I got it with this:

<script>
  selectedBehavior = {
    properties: {
      selected: { 
        type: Object,
        value: function() { return { choice: 'home'} }
      }
    }
  }
</script>

It seems specifying a object is not enough. I need to return a object for the value of object. Doesn't make alot of sense because in documentation I should just be able to say foo: Object. But, maybe this is a special case sense I am using it as a behavior.

Unfortunately, two elements can not share the same obj.property through behaviors. Each will have it's own instance.

Answer:4

I want to load the left half of video texture to left Geometry and right half of video texture to the right Geometry var video = document.createElement("video"); var texture = new THREE.Texture(video)...

I want to load the left half of video texture to left Geometry and right half of video texture to the right Geometry var video = document.createElement("video"); var texture = new THREE.Texture(video)...

  1. three js copy texture

my question is really simple. It is possible use Angular Controllers with prototype? 'use strict'; var EventController = function(scope, EventModel) { this.scope = scope; this....

my question is really simple. It is possible use Angular Controllers with prototype? 'use strict'; var EventController = function(scope, EventModel) { this.scope = scope; this....

  1. in angular controller
  2. angular use controller from another module

I have a little FTP script which basically transfer an entire directory tree (by walking it with fs.readdir) to an FTP server one file at a time (I have to do some analysis on each file as it's ...

I have a little FTP script which basically transfer an entire directory tree (by walking it with fs.readdir) to an FTP server one file at a time (I have to do some analysis on each file as it's ...

  1. node make synchronous http request
  2. node make synchronous query
  3. make node js synchronous
  4. node make function synchronous
  5. node make promise synchronous
  6. make foreach synchronous node js
  7. make request synchronous node js
  8. make node js code synchronous
  9. node make callback synchronous
  10. make function synchronous node js
  11. make for loop synchronous node js
  12. node make asynchronous call synchronous

In my entity framework model (from oracle) I have a table that contains the following Column Now. I use an asp.net webforms datagrid to view the data. At one stage the value in this column is ...

In my entity framework model (from oracle) I have a table that contains the following Column Now. I use an asp.net webforms datagrid to view the data. At one stage the value in this column is ...