简体   繁体   English

将数据传递到a帧中的组件时,有哪些最佳实践?

[英]What are some best practices when passing data to a component in a-frame?

I am quite new to a-frame but am really enjoying creating components and using them as primitives in HTML. 我刚接触A框架,但真的很喜欢创建组件并将其用作HTML中的原语。 In my current project I am creating an a-frame component that creates geometries given a set of vertices. 在我当前的项目中,我正在创建一个a框架组件,该组件会在给定一组顶点的情况下创建几何形状。 I am creating the component in a generic way so that I can pass it different sets of vertices and create different shapes. 我以通用方式创建组件,以便可以将其传递给不同的顶点集并创建不同的形状。

Although this is a specific problem, it speaks to a more general question of what is the suggested way to pass what could be quite big sets of data (objects, arrays) into a component. 尽管这是一个特定的问题,但它涉及到一个更一般的问题,即将可能相当大的数据集(对象,数组)传递到组件中的建议方法是什么。 I would ideally like to be able to pass them from the html layer via a primitive. 理想情况下,我希望能够通过原语从html层传递它们。 The amount of data will differ so I cannot hardcode a different property in the schema per item of data. 数据量会有所不同,因此我无法在每个数据项的架构中对不同的属性进行硬编码。

My current solution is that I have the data available as a global variable. 我当前的解决方案是将数据用作全局变量。 I then pass the name of the variable as a string to the component and convert it to a variable name using window[variablename] . 然后,我将变量名作为字符串传递给组件,并使用window[variablename]将其转换为变量名。 To be clear, this is working fine but I am curious to understand if there is a better way (or even just alternative approaches I might try). 显然,这很好,但是我很好奇是否有更好的方法(或者甚至可以尝试其他方法)。

Please find below an example of what I am doing; 请在下面找到我在做什么的例子;

Here are the bits of data I use in this example; 这是我在此示例中使用的数据位;

var exampleArraydata = [
    {
        "x":3,
        "y":3,
        "z":5
    },
    {
        "x":0,
        "y":15,
        "z":0
    },
    {
        "x":15,
        "y":0,
        "z":0
    }
]

var exampleObjectdata = {
    "x":3,
    "y":3,
    "z":5
}

Here is the example component; 这是示例组件;

AFRAME.registerComponent('examplecomponent', {

    schema: {        
        exampleArray: {type: 'string', default: 'foo'},
        exampleObject: {type: 'string', default: 'bar'},
    },

    init: function (){
       this.testArray = window[this.data.exampleArray]
       this.testObject = window[this.data.exampleObject]
       //logging just to check that it works
       console.log(this.testArray)
       console.log(this.testObject)
       //make geometry with these vec3s...
    }

});

Here is the primitive I create with that component; 这是我使用该组件创建的原语。

AFRAME.registerPrimitive('a-example', {
  defaultComponents: {
    examplecomponent: {}
  },
  mappings: {
    array: 'examplecomponent.exampleArray',
    object: 'examplecomponent.exampleObject'
  }

});

And here is the custom html tag where I pass the variable name as an attribute; 这是自定义html标记,我将变量名作为属性传递给它;

<a-example
    array="exampleArraydata"
    object="exampleObjectdata">
</a-example>

Again, this does work so if people think that is a good approach then great, but I am new to this and if there are any issues with this I would really appreciate understanding what the issues are and ideally some alternative suggestions. 同样,这的确起作用,因此,如果人们认为这是一种好的方法,那就太好了,但是我对此并不陌生,如果对此有任何疑问,我将不胜感激,了解这些问题是什么,最好是一些替代建议。 For example, what if the objects were hosted at a URL rather than as a var in my own code? 例如,如果对象托管在URL中而不是在我自己的代码中作为var托管,该怎么办?

You could also use your own custom parse function to read the data from the HTML attribute in particular way. 您还可以使用自己的自定义解析函数以特定方式从HTML属性读取数据。

Here's an example from the docs: https://aframe.io/docs/0.7.0/core/component.html#custom-property-type 这是docs的示例: https : //aframe.io/docs/0.7.0/core/component.html#custom-property-type

schema: {
  // Parse slash-delimited string to an array (e.g., `foo="myProperty: a/b"` to `['a', 'b']`).
  myProperty: {
    default: [],
    parse: function (value) {
      return value.split('/');
    }
  }
}

Polluting global namespace (window) can lead to problems - you can overwrite important variable one day and it will be difficult to track down, since it won't be undefined . 污染全局名称空间(窗口)可能会导致问题-您有一天可能会覆盖重要的变量,而且由于不会被undefined ,因此很难跟踪。 The suggested way is to pass data via HTML attribute - as you figured out yourself. 建议的方法是通过HTML属性传递数据-如您所知。

Noam Almosnino pointed you in the right direction. Noam Almosnino为您指明了正确的方向。 You can parse the data however you want, if you want to pass an object into component, just parse its' strigified version: 您可以根据需要解析数据,如果要将对象传递给组件,只需解析其分层版本即可:

<a-example objectData="{\"x\":3,\"y\":3,\"z\":5}"></a-example>

schema: {
  objectData: {
    default: {},
    parse: function (str) {
      return JSON.parse(str);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM