javascriptpolymernuxeopolymer-1.x

How to attach nuxeo-tree component to Polymer v1 app


I want to add the <nuxeo-tree> component to my Polymer v1 app, but I'm seeing an error in the console. This is the code I've tried:

<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/nuxeo-ui-elements/nuxeo-tree/nuxeo-tree.html">
<link rel="import" href="./myVerySpecialLib-import.html">

<dom-module id="my-app">
  <template>

    tree:<br/>

    <nuxeo-tree data="[ title: 'root', children: [   {     title: 'a',     children: []   },   {     title: 'b',     children: [       {title: 'x'},       {title: 'y'}     ]   } ]]]" controller="[[controller]">
      <template>
        <template is="dom-if" if="[[!opened]]">
          <iron-icon icon="hardware:keyboard-arrow-right" toggle></iron-icon>
        </template>
        <template is="dom-if" if="[[opened]]">
          <iron-icon icon="hardware:keyboard-arrow-down" toggle></iron-icon>
        </template>
        <span select>My title is: [[item.title]]</span>
        <span>Am I a leaf? [[isLeaf]]</span>
      </template>
    </nuxeo-tree>
  </template>   

  <script>
    Polymer({
      is: 'my-app',

      properties: {
        data: {
            type: String,
            value: "[ title: 'root', children: [{ title: 'a',children: []},{title: 'b',children: [{title: 'x'},{title: 'y'}]}]]",
        },

        opened: {
            type: Boolean,
            value: true,
        },

      },

      controller: {
          // How to get children of a node. Returns a promise.
          getChildren: function(node) {
            return Promise.resolve(node.children);
          },

          // Logics you may want to have to control if a node is a leaf.
          isLeaf: function(node) {
            return node.children.length === 0;
          }
        },

    });

  </script>
</dom-module>

And the myVerySpecialLib-import.html file:

controller = {
  // How to get children of a node. Returns a promise.
  getChildren: function(node) {
    return Promise.resolve(node.children);
  },

  // Logics you may want to have to control if a node is a leaf.
  isLeaf: function(node) {
    return node.children.length === 0;
  }
};

This is the console error:

TypeError: this.controller.isLeaf is not a function

I tried to add the JSON data as a property and also directly into the data field, but neither had a positive effect. How do I fix this?


Solution

  • The myVerySpecialLib-import.html seems to contain a global variable declaration, but that doesn't really help you because <nuxeo-tree> expects controller on the container element (not in a global variable).

    Also, your data binding for <nuxeo-tree>.controller is malformed (it's missing a ] at the end):

    <nuxeo-tree controller="[[controller]">
    

    And controller probably should be declared as a property if you're binding it. It's currently declared outside the properties object.

    // DON'T DO THIS
    /*
    properties: {...},
    controller: {...}
    */
    
    // DO THIS
    properties: {
      controller: {...}
    }
    

    I recommend setting this.controller in the ready() callback of the parent element of <nuxeo-tree> (where this is the container). You could also set <nuxeo-tree>.data via a binding to simplify your HTML template, and that property could be initialized in ready() as well.

    ready: function() {
      this.data = /* insert data object here */;
      this.controller = /* insert controller object here */;
    }
    

    demo