Skip to content

Accessing parent level variables when using c-bind to loop through an array #6

@joncorrin

Description

@joncorrin

I just started using Crufltess to integrate with another vendor who requires a strict XML file using SOAP and file upload. Cruftless has proved to work well for this. I noticed two things I wanted to report which I think should be either addressed in the readme or possibly enhanced. Both have to do with the looping using c-bind or alike

Issue 1: I can't use c-bind and c-if in the same line
I now realize that c-bind already detects if the array exists so theres no need to use c-if. I think this should be addressed in the readme under the c-if section. This is not to be used with c-bind (unless I'm incorrect on this).

Issue 2: I can't access parent level variables in the array itself (or I can't figure out how to).

Following Issue 2: In my work I had an object structured like this:

const obj = { Drivers: [ {id: 1} ], Violations: [ {id: 1} ] }

But my schema was structured like this:

const template = `
<Drivers>
  <Driver c-bind="Drivers|array">
      <ID>{id}</ID>
      <Violations c-bind="Violations"><ID>{id}</ID></Violations>
  </Driver>
</Drivers>`

Which ended up printing this :

const {  parse  } = require('cruftless');
const xmlModel = parse(template);
const xml = xmlModel.toXML(obj);

console.log(xml);
/*
<Drivers>
  <Driver>
      <ID>1</ID>
  </Driver>
</Drivers>
*/

I realize that Cruftless is looking through the Drivers array of Driver objects for a Violations variable, I also realize that moving Violations into the Driver object could solve this problem, but given the way the data is structured it'd be easier for me to be able to access the parent level Violations and loop through it inside the Drivers element when the XML is printed.

Is there a way to achieve this? Accessing a parent level variable in an array?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions