2017-03-17 43 views
1

我正在写一个自定义框架组件来渲染一个基于很长的对象数组的网格。如何将引用传递给aframe组件?

AFRAME文档仅列出数组作为输入类型,其中可以传递一个属性,它将被解析到一个数组attributename="1 2 3"

我想通过与这样的一个javascript参考从外部组件:

const hugeArray = [{somejson}, ...] 
const element = document.createElement('my-component'); 
element.<something happens here> 

或DOM API之外创建组件和参数传递给组件的init方法。

回答

1

使用setAttribute,这可能需要对象和数组为好。通过schema而不是调用方法,因为init处理程序将在正确的时间自动为您调用。

https://aframe.io/docs/0.5.0/core/entity.html#setattribute-attr-value-componentattrvalue

AFRAME.registerComponent('mycomponent', { 
    schema: { 
    yourData: {type: 'array'} 
    }, 

    init: function() { 
    console.log(this.data.yourData); 
    } 
}); 

const hugeArray = [{somejson}, ...] 
const element = document.createElement('a-entity'); 
element.setAttribute('mycomponent', {yourData: hugeArray}); 
scene.appendChild(element); 
+0

同意,这是一个更清洁的选择。 – naugtur

0

找到一种方法来做到这一点。

const hugeArray = [{somejson}, ...] 
const element = document.createElement('a-entity'); 
element.setAttribute('mycomponent', ''); 
//... add component to DOM and let it initialize 
element.components.mycomponent.customMethod(hugeArray); 

所有这一切都假定组件根据一个名称“myComponent的”已登记的和有一个方法customMethod旁边初始化等