dont do async: false (#731)

* dont do async: false

* account for case where callback isn't provided
This commit is contained in:
Connor Turland 2016-10-06 09:07:46 -04:00 committed by GitHub
parent d80d33761d
commit a79d6a824c
6 changed files with 61 additions and 137 deletions

View file

@ -49,7 +49,7 @@ const AutoLayout = {
} }
} }
if (opts.map && self.coordsTaken(nextX, nextY, opts.map)) { if (opts.mappings && self.coordsTaken(nextX, nextY, opts.mappings)) {
// check if the coordinate is already taken on the current map // check if the coordinate is already taken on the current map
return self.getNextCoord(opts) return self.getNextCoord(opts)
} else { } else {
@ -59,8 +59,7 @@ const AutoLayout = {
} }
} }
}, },
coordsTaken: function (x, y, map) { coordsTaken: function (x, y, mappings) {
const mappings = map.getMappings()
if (mappings.findWhere({ xloc: x, yloc: y })) { if (mappings.findWhere({ xloc: x, yloc: y })) {
return true return true
} else { } else {

View file

@ -85,46 +85,6 @@ _Backbone.Map = Backbone.Model.extend({
getUser: function () { getUser: function () {
return Mapper.get(this.get('user_id')) return Mapper.get(this.get('user_id'))
}, },
fetchContained: function () {
var bb = _Backbone
var that = this
var start = function (data) {
that.set('mappers', new bb.MapperCollection(data.mappers))
that.set('topics', new bb.TopicCollection(data.topics))
that.set('synapses', new bb.SynapseCollection(data.synapses))
that.set('mappings', new bb.MappingCollection(data.mappings))
}
$.ajax({
url: '/maps/' + this.id + '/contains.json',
success: start,
async: false
})
},
getTopics: function () {
if (!this.get('topics')) {
this.fetchContained()
}
return this.get('topics')
},
getSynapses: function () {
if (!this.get('synapses')) {
this.fetchContained()
}
return this.get('synapses')
},
getMappings: function () {
if (!this.get('mappings')) {
this.fetchContained()
}
return this.get('mappings')
},
getMappers: function () {
if (!this.get('mappers')) {
this.fetchContained()
}
return this.get('mappers')
},
updateView: function () { updateView: function () {
var map = Active.Map var map = Active.Map
var isActiveMap = this.id === map.id var isActiveMap = this.id === map.id

View file

@ -227,7 +227,7 @@ const Import = {
parsedTopics.forEach(function (topic) { parsedTopics.forEach(function (topic) {
let coords = { x: topic.x, y: topic.y } let coords = { x: topic.x, y: topic.y }
if (!coords.x || !coords.y) { if (!coords.x || !coords.y) {
coords = AutoLayout.getNextCoord({ map: Active.Map }) coords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings })
} }
if (!topic.name && topic.link || if (!topic.name && topic.link ||
@ -353,7 +353,7 @@ const Import = {
handleURL: function (url, opts = {}) { handleURL: function (url, opts = {}) {
let coords = opts.coords let coords = opts.coords
if (!coords || coords.x === undefined || coords.y === undefined) { if (!coords || coords.x === undefined || coords.y === undefined) {
coords = AutoLayout.getNextCoord({ map: Active.Map }) coords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings })
} }
const name = opts.name || 'Link' const name = opts.name || 'Link'

View file

@ -981,7 +981,6 @@ const Realtime = {
else if (!couldEditBefore && canEditNow) { else if (!couldEditBefore && canEditNow) {
Map.canEditNow() Map.canEditNow()
} else { } else {
model.fetchContained()
model.trigger('changeByOther') model.trigger('changeByOther')
} }
} }

View file

@ -19,35 +19,21 @@ import Visualize from './Visualize'
* - Metamaps.Topics * - Metamaps.Topics
*/ */
const noOp = () => {}
const Synapse = { const Synapse = {
// this function is to retrieve a synapse JSON object from the database // this function is to retrieve a synapse JSON object from the database
// @param id = the id of the synapse to retrieve // @param id = the id of the synapse to retrieve
get: function (id, callback) { get: function (id, callback = noOp) {
// if the desired topic is not yet in the local topic repository, fetch it // if the desired topic is not yet in the local topic repository, fetch it
if (Metamaps.Synapses.get(id) == undefined) { if (Metamaps.Synapses.get(id) == undefined) {
if (!callback) { $.ajax({
var e = $.ajax({ url: '/synapses/' + id + '.json',
url: '/synapses/' + id + '.json', success: function (data) {
async: false Metamaps.Synapses.add(data)
}) callback(Metamaps.Synapses.get(id))
Metamaps.Synapses.add($.parseJSON(e.responseText)) }
return Metamaps.Synapses.get(id) })
} else { } else callback(Metamaps.Synapses.get(id))
return $.ajax({
url: '/synapses/' + id + '.json',
success: function (data) {
Metamaps.Synapses.add(data)
callback(Metamaps.Synapses.get(id))
}
})
}
} else {
if (!callback) {
return Metamaps.Synapses.get(id)
} else {
return callback(Metamaps.Synapses.get(id))
}
}
}, },
/* /*
* *
@ -152,21 +138,19 @@ const Synapse = {
node1, node1,
node2 node2
var synapse = self.get(id) self.get(id, synapse => {
var mapping = new Metamaps.Backbone.Mapping({
var mapping = new Metamaps.Backbone.Mapping({ mappable_type: 'Synapse',
mappable_type: 'Synapse', mappable_id: synapse.id,
mappable_id: synapse.id, })
Metamaps.Mappings.add(mapping)
topic1 = Metamaps.Topics.get(Create.newSynapse.topic1id)
node1 = topic1.get('node')
topic2 = Metamaps.Topics.get(Create.newSynapse.topic2id)
node2 = topic2.get('node')
Create.newSynapse.hide()
self.renderSynapse(mapping, synapse, node1, node2, true)
}) })
Metamaps.Mappings.add(mapping)
topic1 = Metamaps.Topics.get(Create.newSynapse.topic1id)
node1 = topic1.get('node')
topic2 = Metamaps.Topics.get(Create.newSynapse.topic2id)
node2 = topic2.get('node')
Create.newSynapse.hide()
self.renderSynapse(mapping, synapse, node1, node2, true)
} }
} }

View file

@ -28,37 +28,21 @@ import Visualize from './Visualize'
* - Metamaps.Synapses * - Metamaps.Synapses
* - Metamaps.Topics * - Metamaps.Topics
*/ */
const noOp = () => {}
const Topic = { const Topic = {
// this function is to retrieve a topic JSON object from the database // this function is to retrieve a topic JSON object from the database
// @param id = the id of the topic to retrieve // @param id = the id of the topic to retrieve
get: function (id, callback) { get: function (id, callback = noOp) {
// if the desired topic is not yet in the local topic repository, fetch it // if the desired topic is not yet in the local topic repository, fetch it
if (Metamaps.Topics.get(id) == undefined) { if (Metamaps.Topics.get(id) == undefined) {
// console.log("Ajax call!") $.ajax({
if (!callback) { url: '/topics/' + id + '.json',
var e = $.ajax({ success: function (data) {
url: '/topics/' + id + '.json', Metamaps.Topics.add(data)
async: false callback(Metamaps.Topics.get(id))
}) }
Metamaps.Topics.add($.parseJSON(e.responseText)) })
return Metamaps.Topics.get(id) } else callback(Metamaps.Topics.get(id))
} else {
return $.ajax({
url: '/topics/' + id + '.json',
success: function (data) {
Metamaps.Topics.add(data)
callback(Metamaps.Topics.get(id))
}
})
}
} else {
if (!callback) {
return Metamaps.Topics.get(id)
} else {
return callback(Metamaps.Topics.get(id))
}
}
}, },
launch: function (id) { launch: function (id) {
var bb = Metamaps.Backbone var bb = Metamaps.Backbone
@ -192,7 +176,7 @@ const Topic = {
}, },
// opts is additional options in a hash // opts is additional options in a hash
// TODO: move createNewInDB and permitCerateSYnapseAfter into opts // TODO: move createNewInDB and permitCreateSynapseAfter into opts
renderTopic: function (mapping, topic, createNewInDB, permitCreateSynapseAfter, opts = {}) { renderTopic: function (mapping, topic, createNewInDB, permitCreateSynapseAfter, opts = {}) {
var self = Topic var self = Topic
@ -335,7 +319,7 @@ const Topic = {
Metamaps.Topics.add(topic) Metamaps.Topics.add(topic)
if (Create.newTopic.pinned) { if (Create.newTopic.pinned) {
var nextCoords = AutoLayout.getNextCoord() var nextCoords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings })
} }
var mapping = new Metamaps.Backbone.Mapping({ var mapping = new Metamaps.Backbone.Mapping({
xloc: nextCoords ? nextCoords.x : Create.newTopic.x, xloc: nextCoords ? nextCoords.x : Create.newTopic.x,
@ -357,40 +341,38 @@ const Topic = {
Create.newTopic.hide() Create.newTopic.hide()
var topic = self.get(id) self.get(id, (topic) => {
if (Create.newTopic.pinned) {
var nextCoords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings })
}
var mapping = new Metamaps.Backbone.Mapping({
xloc: nextCoords ? nextCoords.x : Create.newTopic.x,
yloc: nextCoords ? nextCoords.y : Create.newTopic.y,
mappable_type: 'Topic',
mappable_id: topic.id,
})
Metamaps.Mappings.add(mapping)
if (Create.newTopic.pinned) { self.renderTopic(mapping, topic, true, true)
var nextCoords = AutoLayout.getNextCoord()
}
var mapping = new Metamaps.Backbone.Mapping({
xloc: nextCoords ? nextCoords.x : Create.newTopic.x,
yloc: nextCoords ? nextCoords.y : Create.newTopic.y,
mappable_type: 'Topic',
mappable_id: topic.id,
}) })
Metamaps.Mappings.add(mapping)
self.renderTopic(mapping, topic, true, true)
}, },
getTopicFromSearch: function (event, id) { getTopicFromSearch: function (event, id) {
var self = Topic var self = Topic
$(document).trigger(Map.events.editedByActiveMapper) $(document).trigger(Map.events.editedByActiveMapper)
var topic = self.get(id) self.get(id, (topic) => {
var nextCoords = AutoLayout.getNextCoord({ mappings: Metamaps.Mappings })
var nextCoords = AutoLayout.getNextCoord() var mapping = new Metamaps.Backbone.Mapping({
var mapping = new Metamaps.Backbone.Mapping({ xloc: nextCoords.x,
xloc: nextCoords.x, yloc: nextCoords.y,
yloc: nextCoords.y, mappable_type: 'Topic',
mappable_type: 'Topic', mappable_id: topic.id,
mappable_id: topic.id, })
Metamaps.Mappings.add(mapping)
self.renderTopic(mapping, topic, true, true)
GlobalUI.notifyUser('Topic was added to your map!')
}) })
Metamaps.Mappings.add(mapping)
self.renderTopic(mapping, topic, true, true)
GlobalUI.notifyUser('Topic was added to your map!')
event.stopPropagation() event.stopPropagation()
event.preventDefault() event.preventDefault()