Vue为"关系"添加现有的Firebase ID

时间:2017-05-03 23:15:34

标签: javascript firebase firebase-realtime-database vuejs2 vuefire

我通过VueFire使用Vue和Firebase来构建一个表单来关联对象。我有以下形式和方法:

<form @submit.prevent="addRelation">
      <select v-model="newRelation.parent">
        <option v-for="item in content">{{item.name}}</option>
      </select>
      <select v-model="newRelation.child">
        <option v-for="item in content">{{item.name}}</option>
      </select>
      <input type="submit" value="Add Relation"/>
    </form>

和Vue:

new Vue({
    el: "#app",
    data: {
        related: {},
        newRelation: {
            parent: '',
            child: ''
        }
    },
    firebase: {
        content: contentRef,
        related: relatedRef
    },
    methods: {
        addRelation() {
            relatedRef.push(this.newRelation)
            console.log('relation added')
        }
    },

这会在Firebase中添加JSON:

{
    "child": "objectc",
    "parent": "objecta",
    ".key": "-KjFYZn8dqP29uLmJBJP"
  },

它会推送一个新的Firebase ID并嵌套数据。为了使Firebase能够处理关系&#39;正确地说,它们的结构必须如此:

{
    ".key":"-Existing ID of object A
    "-Existing ID of object B": true
}

如何将我的方法更新为结构信息?我有CodePen&#34;&#39;相关&#39;中的最后两个条目。内容&#34;部分显示我希望它如何提交(我手动添加)。

0 个答案:

没有答案