samedi 14 novembre 2015

Ember editable recursive nested components

I'm currently trying to build a component that will accept a model like this

      "values": {
        "value1": 234,
        "valueOptions": {
          "subOption1": 123,
          "subOption2": 133,
          "subOption3": 7432,
          "valueOptions2": {
            "subSubOption4": 821
          }
        }
      }  

with each object recursively creating a new component. So far I've created this branch and node components and its fine at receiving the data and displaying it but the problem I'm having is how I can edit and save the data. Each component has a different data set as it is passed down its own child object.

tree-branch component template:

{{#each children as |child|}}
{{child.name}} 
{{tree-node node=child.value}}
 {{/each}}

 {{#each items as |item|}}
  <li>{{input value=item.key}} : {{input value=item.value}} <button {{action 'save' item}}>Save</button></li>
{{/each}}

tree-branch component controller:

export default Ember.Component.extend({
tagName: 'li',
classNames: ['branch'],

items: function() {
    var node = this.get('node')

    var keys = Object.keys(node);

    return keys.filter(function(key) {
        return node[key].constructor !== Object
    }).map(function(key){
        return { key: key, value: node[key]};
    })

}.property('node'),

children : function() {

    var node = this.get('node');
    var children = [];
    var keys = Object.keys(node);

    var branchObjectKeys = keys.filter(function(key) {
        return node[key].constructor === Object
    })

    branchObjectKeys.forEach(function(keys) {
        children.push(keys)
    })

    children = children.map(function(key) {
        return {name:key, value: node[key]}
    })

    return children

    }.property('node'),


    actions:  {
        save: function(item) {
            console.log(item.key, item.value);
        }
    }

});

tree-node component:

{{tree-branch node=node}}

Anyone who has any ideas of how I can get this working would be a major help, thanks!

Aucun commentaire:

Enregistrer un commentaire