逻辑和数据的分离

如果需要,系统可以帮助将逻辑和行为与数据分开。我们让系统处理繁重的工作,组件只担心通过其生命周期方法管理数据:

AFRAME.registerSystem('my-component', {
  createComplexObject: function (data) {
    // Do calculations and stuff with data.
    return new ComplexObject(data);
  }
});

AFRAME.registerComponent('my-component', {
  init: function () {
    this.myObject = null;
  },

  update: function () {
    // Do stuff with `this.data`.
    this.myObject = this.system.createComplexObject(this.data);
  }
});