|
| 1 | +define(['jquery', 'backbone'], function($, Backbone) { |
| 2 | + var Channel, namespace, nsref; |
| 3 | + |
| 4 | + /** |
| 5 | + NOTE: Remeber you are using my application sample_app_with_d3. |
| 6 | + Appbase is completely free up to 100 thousand API calls per month. |
| 7 | + Feel free to use it while you're learning. |
| 8 | + After that, create your own application's name, |
| 9 | + then new learners can use my API calls left. Thanks. |
| 10 | + **/ |
| 11 | + Appbase.credentials("pubsub_with_backbone_events", "fa97bda44848217908a1a2a3ccf7bf33"); |
| 12 | + namespace = "my-first-namespace"; |
| 13 | + nsref = Appbase.ns(namespace); |
| 14 | + /* END */ |
| 15 | + |
| 16 | + Channel = $.extend( {}, Backbone.Events ); |
| 17 | + |
| 18 | + Channel.on('disconnect', function() { |
| 19 | + nsref.off('vertex_added'); |
| 20 | + nsref.off('vertex_removed'); |
| 21 | + }); |
| 22 | + |
| 23 | + Channel.on('retrieve-all-nodes', function() { |
| 24 | + nsref.on('vertex_added', function(err, vertexRef, obj) { |
| 25 | + |
| 26 | + Channel.trigger( 'node-added', $.extend( obj.properties() || {}, { |
| 27 | + id: vertexRef.name() |
| 28 | + } ) ); |
| 29 | + |
| 30 | + vertexRef.on('properties', function(error, ref, snapObj) { |
| 31 | + Channel.trigger( 'node-edited', snapObj.properties() ); |
| 32 | + }); |
| 33 | + |
| 34 | + vertexRef.on('edge_added', function(error, edgeRef, snapObj) { |
| 35 | + vertexRef.once('properties', function(error_source, ref_target, obj_target) { |
| 36 | + edgeRef && edgeRef.once('properties', function(error_target, ref_source, obj_source) { |
| 37 | + Channel.trigger( 'link-added', { |
| 38 | + source: obj_source.properties(), |
| 39 | + target: obj_target.properties(), |
| 40 | + id: edgeRef.name() |
| 41 | + }); |
| 42 | + }); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + vertexRef.on('edge_removed', function(error, edgeRef, snapObj) { |
| 47 | + Channel.trigger( 'link-removed' , {id: edgeRef.name()}); |
| 48 | + }); |
| 49 | + |
| 50 | + }); |
| 51 | + |
| 52 | + nsref.on('vertex_removed', function(err, vertexRef, obj) { |
| 53 | + Channel.trigger( 'node-removed', obj.properties() ); |
| 54 | + }); |
| 55 | + |
| 56 | + }); |
| 57 | + |
| 58 | + Channel.on('add-node', function( node, cb ) { |
| 59 | + var id = Appbase.uuid(), vref = nsref.v(id); |
| 60 | + node.id = id; |
| 61 | + vref.setData(node); |
| 62 | + |
| 63 | + if (cb) { |
| 64 | + cb(node); |
| 65 | + } else { |
| 66 | + Channel.trigger( 'node-added', node ); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + Channel.on('edit-node', function(node) { |
| 71 | + if (node && node.id) { |
| 72 | + nsref.v(node.id).setData(node); |
| 73 | + Channel.trigger( 'node-edited', node ); |
| 74 | + } |
| 75 | + } ); |
| 76 | + |
| 77 | + Channel.on('remove-node', function(node) { |
| 78 | + if (node && node.id) { |
| 79 | + nsref.v(node.id).destroy(); |
| 80 | + Channel.trigger( 'node-removed', node.id ); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + Channel.on('add-link', function(link) { |
| 85 | + var source, target, id; |
| 86 | + if (!link || !link.source || !link.target || !link.source.id || !link.target.id) return; |
| 87 | + source = nsref.v(link.source.id); |
| 88 | + target = nsref.v(link.target.id); |
| 89 | + id = Appbase.uuid(); |
| 90 | + source.setEdge( id, target, function(error) { |
| 91 | + if (error) { |
| 92 | + console.error( 'add-link', error ); |
| 93 | + } else { |
| 94 | + link.id = id; |
| 95 | + Channel.trigger( 'link-added', link ); |
| 96 | + } |
| 97 | + } ); |
| 98 | + }); |
| 99 | + |
| 100 | + Channel.on('remove-link', function(link) { |
| 101 | + if (link && link.id) { |
| 102 | + nsref.v( link.target.id ).removeEdge( [link.id] ); |
| 103 | + Channel.trigger( 'link-removed', link ); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + return Channel; |
| 108 | +}); |
| 109 | + |
0 commit comments