diff --git a/core/block.js b/core/block.js index c16129a39..58bc311a4 100644 --- a/core/block.js +++ b/core/block.js @@ -697,7 +697,7 @@ Blockly.Block.prototype.getVarModels = function() { if (field instanceof Blockly.FieldVariable) { // TODO (#1199): When we switch to tracking variables by ID, // update this. - var model = this.workspace.getVariable(field.getValue(), ''); + var model = this.workspace.getVariableById(field.getValue()); if (model) { vars.push(model); } diff --git a/core/workspace.js b/core/workspace.js index a8f7eccfa..6055dc4d0 100644 --- a/core/workspace.js +++ b/core/workspace.js @@ -355,6 +355,29 @@ Blockly.Workspace.prototype.getVariableUses = function(name, opt_type) { return uses; }; +/** + * Find all the uses of a named variable. + * TODO (#1199): Possibly delete this function. + * @param {string} id ID of the variable to find. + * @return {!Array.} Array of block usages. + */ +Blockly.Workspace.prototype.getVariableUsesById = function(id) { + var uses = []; + var blocks = this.getAllBlocks(); + // Iterate through every block and check the name. + for (var i = 0; i < blocks.length; i++) { + var blockVariables = blocks[i].getVarModels(); + if (blockVariables) { + for (var j = 0; j < blockVariables.length; j++) { + if (blockVariables[j].getId() == id) { + uses.push(blocks[i]); + } + } + } + } + return uses; +}; + /** * Delete a variable by the passed in name and all of its uses from this * workspace. May prompt the user for confirmation. @@ -419,7 +442,7 @@ Blockly.Workspace.prototype.deleteVariableById = function(id) { * @private */ Blockly.Workspace.prototype.deleteVariableInternal_ = function(variable) { - var uses = this.getVariableUses(variable.name); + var uses = this.getVariableUsesById(variable.getId()); Blockly.Events.setGroup(true); for (var i = 0; i < uses.length; i++) { uses[i].dispose(true, false);