From f9e62496157264e8fba08191f61add2d57f13470 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sun, 27 Mar 2016 11:46:58 +0800 Subject: [PATCH 1/2] Fix up import - want more backboney event listening though --- .../javascripts/src/Metamaps.Import.js.erb | 22 ++++++++++++++----- app/assets/javascripts/src/Metamaps.js.erb | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/src/Metamaps.Import.js.erb b/app/assets/javascripts/src/Metamaps.Import.js.erb index bfce14bd..9558653f 100644 --- a/app/assets/javascripts/src/Metamaps.Import.js.erb +++ b/app/assets/javascripts/src/Metamaps.Import.js.erb @@ -34,14 +34,17 @@ Metamaps.Import = { init: function() { var self = Metamaps.Import; + $('body').bind('paste', function(e) { + if (e.target.tagName === "INPUT") return; + var text = e.originalEvent.clipboardData.getData('text/plain'); var results; if (text[0] === '{') { try { results = JSON.parse(text); - } catch (Error e) { + } catch (e) { results = false; } } else { @@ -63,7 +66,6 @@ Metamaps.Import = { }, abort: function(message) { - alert("Sorry, something went wrong!\n\n" + message); console.error(message); }, @@ -283,20 +285,28 @@ Metamaps.Import = { var topic2 = Metamaps.Topics.get(self.cidMappings[node2_id]); var node1 = topic1.get('node'); var node2 = topic2.get('node'); - // TODO check if topic1 and topic2 were sucessfully found... + + if (topic1.isNew() || topic2.isNew()) { + return setTimeout(function() { + self.createSynapseWithParameters(description, category, permission, + node1_id, node2_id) + }, 200); + } var synapse = new Metamaps.Backbone.Synapse({ desc: description, category: category, permission: permission, - node1_id: node1.id, - node2_id: node2.id, + node1_id: topic1.id, + node2_id: topic2.id }); + Metamaps.Synapses.add(synapse); var mapping = new Metamaps.Backbone.Mapping({ mappable_type: "Synapse", - mappable_id: synapse.id, + mappable_id: synapse.cid, }); + Metamaps.Mappings.add(mapping); Metamaps.Synapse.renderSynapse(mapping, synapse, node1, node2, true); }, diff --git a/app/assets/javascripts/src/Metamaps.js.erb b/app/assets/javascripts/src/Metamaps.js.erb index 85b12d48..7a9d803b 100644 --- a/app/assets/javascripts/src/Metamaps.js.erb +++ b/app/assets/javascripts/src/Metamaps.js.erb @@ -4683,7 +4683,7 @@ Metamaps.Synapse = { node1 = synapsesToCreate[i]; topic1 = node1.getData('topic'); synapse = new Metamaps.Backbone.Synapse({ - desc: Metamaps.Create.newSynapse.description,// || "", + desc: Metamaps.Create.newSynapse.description, node1_id: topic1.isNew() ? topic1.cid : topic1.id, node2_id: topic2.isNew() ? topic2.cid : topic2.id, }); From 30d327f07aec12efb079d258735671d1dc1c396c Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sun, 27 Mar 2016 15:20:09 +0800 Subject: [PATCH 2/2] solution using backbone events instead of setTimeout --- .../javascripts/src/Metamaps.Import.js.erb | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/src/Metamaps.Import.js.erb b/app/assets/javascripts/src/Metamaps.Import.js.erb index 9558653f..f30f65e5 100644 --- a/app/assets/javascripts/src/Metamaps.Import.js.erb +++ b/app/assets/javascripts/src/Metamaps.Import.js.erb @@ -70,7 +70,7 @@ Metamaps.Import = { }, simplify: function(string) { - return string + return string; .replace(/(^\s*|\s*$)/g, '') .toLowerCase(); }, @@ -239,10 +239,28 @@ Metamaps.Import = { var self = Metamaps.Import; parsedSynapses.forEach(function(synapse) { - self.createSynapseWithParameters( - synapse.desc, synapse.category, synapse.permission, - synapse.topic1, synapse.topic2 - ); + //only createSynapseWithParameters once both topics are persisted + var topic1 = Metamaps.Topics.get(self.cidMappings[node1_id]); + var topic2 = Metamaps.Topics.get(self.cidMappings[node2_id]); + var synapse_created = false + topic1.once('sync', function() { + if (topic1.id && topic2.id && !synapse_created) { + synaprse_created = true + self.createSynapseWithParameters( + synapse.desc, synapse.category, synapse.permission, + topic1, topic2 + ); + }//if + }); + topic2.once('sync', function() { + if (topic1.id && topic2.id && !synapse_created) { + synaprse_created = true + self.createSynapseWithParameters( + synapse.desc, synapse.category, synapse.permission, + topic1, topic2 + ); + }//if + }); }); }, @@ -279,19 +297,15 @@ Metamaps.Import = { }, createSynapseWithParameters: function(description, category, permission, - node1_id, node2_id) { + topic1, topic2) { var self = Metamaps.Import; - var topic1 = Metamaps.Topics.get(self.cidMappings[node1_id]); - var topic2 = Metamaps.Topics.get(self.cidMappings[node2_id]); var node1 = topic1.get('node'); var node2 = topic2.get('node'); - if (topic1.isNew() || topic2.isNew()) { - return setTimeout(function() { - self.createSynapseWithParameters(description, category, permission, - node1_id, node2_id) - }, 200); - } + if (!topic1.id || !topic2.id) { + console.error("missing topic id when creating synapse") + return; + }//if var synapse = new Metamaps.Backbone.Synapse({ desc: description,