编程语言
首页 > 编程语言> > javascript-如何使用Neo4j Reactivity驱动程序发布/订阅Meteor

javascript-如何使用Neo4j Reactivity驱动程序发布/订阅Meteor

作者:互联网

我正在使用Meteor应用程序,您可以在其中创建Neo4j“ Room”节点和“ DOOR”关系.每个房间只能有一个通往其他房间的门.

我正在使用Dmitriy Aristarkhovich的Neo4j Reactivity驱动程序.

我想创建一个实时的主从系统,其中用户做出的选择会影响其他可能的选择.

为了说明这一点,在端口7474上的Neo4j浏览器中,我创建了一个简单的房间网络,如下所示:

CREATE
(a:Room {name: "Room 1"})
-[:DOOR]->
(b:Room {name: "Room 2"}),
a-[:DOOR]->
(c:Room {name: "Room 3"}),
b-[:DOOR]->a
RETURN a, b, c

Rooms and doors

在此架构中:

> 1号房已经拥有它可以拥有的所有出门
> 2号房间可以通过门链接到3号房间
> 3号房间可以同时链接到1号房间和2号房间

我想显示两个下拉菜单:

>主菜单选择门的起步室
>从属菜单,从给定起始房间的可能的结束房间中进行选择.

这是我用来创建这些菜单的流星风味HTML:

<body>
  {{> addDoor}}
</body>

<template name="addDoor">
  <p>Add a Door from
    <select id="fromRoomName">
      {{#each fromRooms}}
        {{> fromRoom}}
      {{/each}}
    </select>

    to: 
    <select id="toRoomName">
      {{#each toRooms}}
        {{> toRoom}}
      {{/each}}
    </select>
  </p>
</template>

<template name="fromRoom">
  <option value="{{name}}">{{name}}</option>
</template>

<template name="toRoom">
  <option value="{{name}}">{{name}}</option>
</template>

在主菜单中选择Room 3时,这些菜单应该是这样.

Master-slave menus

这是我正在使用的JavaScript:

var queries = {
  "roomsForNewDoors": {
    collection: Meteor.neo4j.collection("RoomsForNewDoors")
  , query: 
     "MATCH (entrance:Room), (exit:Room) " +
     "WHERE NOT (entrance)-[:DOOR]->(exit) " +
     "AND entrance <> exit " +
     "RETURN entrance"
  , link: "entrance"
  }
, "newDoorsForRoom": {
    collection: Meteor.neo4j.collection("NewDoorsForRoom")
  , query: 
      "MATCH (entrance:Room), (exit:Room) " +
      "WHERE entrance.name = '{name}' " +
      "AND NOT (entrance)-[:DOOR]->(exit) " +
      "AND entrance <> exit " +
      "RETURN exit"
  , options: {name: "Room 2"}
  , link: "exit"
  }
}


if (Meteor.isServer) {
  ;(function (){
    var queryKeys = Object.keys(queries)
    queryKeys.forEach(publish)

    function publish(key) {
      var queryData = queries[key]
      var query = queryData.query
      var collection = queryData.collection

      collection.publish(key, publishCallback)

      function publishCallback(){
        return query
      }
    }
  })()
}


if (Meteor.isClient) {
  Tracker.autorun(function createSubscriptions(){
    var queryKeys = Object.keys(queries)
    queryKeys.forEach(subscribe)

    function subscribe(key) { //, index, array){
      var queryData = queries[key]
      var options = queryData.options
      var link = queryData.link
      var collection = queryData.collection
      var subscription = collection.subscribe(key, options, link)
    }
  })

  function getResults(queryData) {
    var collection = queryData.collection
    var cursor = collection.find()
    return cursor
  }

  Template.addDoor.events({
    'change #fromRoomName': function () {
      var fromRoomId = $("#fromRoomName :selected").text()
      Session.set("fromRoomId", fromRoomId)
    }
  })

  Template.addDoor.helpers({
    fromRooms: function() {
      var results = getResults(queries.roomsForNewDoors)
      return results
    }

  , toRooms: function () {
      var key = "newDoorsForRoom"
      var queryData = queries[key]
      var options = { name: Session.get("fromRoomId") }
      var link = queryData.link
      var collection = queryData.collection
      collection.subscribe(key, options, link)

      var results = getResults(queryData)
      return results
    }
  })
}

当我在Neo4j浏览器中测试两个MATCH查询时(例如,使用“ Room 2”而不是“ {name}”),我得到的正是我期望的结果.使用Neo4j Reactivity驱动程序,第一个MATCH查询运行良好,但是第二个查询会产生一组空结果.

我怀疑我不了解发布/订阅过程的一些基本知识.我将不胜感激能使该功能正常工作.

完整的项目在这里可用:

git clone git@github.com:blackslate/Neo4j-issues.git

解决方法:

这可能不是发布/订阅问题.尝试删除{name}参数周围的引号.

也就是说,更改:

"WHERE entrance.name = '{name}' "

至:

"WHERE entrance.name = {name} ".

标签:neo4j,meteor,reactive-programming,master-slave,javascript
来源: https://codeday.me/bug/20191027/1948326.html