index.js 1.46 KB
// 商品card
Component({
  externalClasses: ['custom-class', 'active-class', 'sidebar-active-custom'],
  relations: {
    '../sidebar/index': {
      type: 'parent',
      linked: function () {
        // 每次被插入到 custom-ul 时执行,target是 custom-ul 节点实例对象,触发在 attached 生命周期之后
      },
      linkChanged: function () {
        // 每次被移动后执行,target是 custom-ul 节点实例对象,触发在 moved 生命周期之后
      },
      unlinked: function () {
        // 每次被移除时执行,target是 custom-ul 节点实例对象,触发在 detached 生命周期之后
      }

    }
  },
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    selected: ''
  },
  methods: {
    // 设置选中
    setActive: function (selected) {
      return this.setData({selected: selected});
    },

    // 点击事件
    onClick() {
      const _this = this;
      const parent = this.getParents();
      if (!parent || this.data.disabled) {
        return;
      }
      const index = parent.data.children.indexOf(this);
      parent.setActive(index)
      _this.triggerEvent('click', index);
      parent.triggerEvent('change', index);
    },

    // 获取父级元素
    getParents() {
      const nodes = this.getRelationNodes('../sidebar/index')
      const node = nodes[0]
      return node;
    }

  }

})