From f9385d98d3df192c009bc155eedc747ffbcb2193 Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Wed, 2 Nov 2016 16:52:37 -0700 Subject: [PATCH 1/2] Add ability to define blocks with a json array --- core/blockly.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/core/blockly.js b/core/blockly.js index 3d33d8e8c..9fa5966cb 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -399,6 +399,33 @@ Blockly.prompt = function(message, defaultValue, callback) { callback(window.prompt(message, defaultValue)); }; +/** + * Helper function for defining a block from json. The resulting function has + * the correct value of jsonDef at the point in code where jsonInit is called. + * @param {Object} jsonDef The JSON definition of a block. + * @return {function} A function that calls jsonInit with the correct value + * of jsonDef. + */ +Blockly.jsonInitFactory_ = function(jsonDef) { + return function() { + this.jsonInit(jsonDef); + }; +}; + +/** + * Define blocks from an array of JSON block definitions, as might be generated + * by the Blockly Developer Tools. + * @param {!Array} jsonArray An array of JSON block definitions. + */ +Blockly.defineBlocksWithJsonArray = function(jsonArray) { + for (var index = 0; index < jsonArray.length; index++) { + var elem = jsonArray[index]; + Blockly.Blocks[elem.type] = { + init: Blockly.jsonInitFactory_(elem) + }; + } +}; + // IE9 does not have a console. Create a stub to stop errors. if (!goog.global['console']) { goog.global['console'] = { From 26c10fe585c189df81136a6385ffc63d653f47de Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Wed, 9 Nov 2016 13:42:58 -0800 Subject: [PATCH 2/2] lint --- core/blockly.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/blockly.js b/core/blockly.js index 9fa5966cb..eb7eb75b0 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -400,11 +400,12 @@ Blockly.prompt = function(message, defaultValue, callback) { }; /** - * Helper function for defining a block from json. The resulting function has + * Helper function for defining a block from JSON. The resulting function has * the correct value of jsonDef at the point in code where jsonInit is called. - * @param {Object} jsonDef The JSON definition of a block. + * @param {!Object} jsonDef The JSON definition of a block. * @return {function} A function that calls jsonInit with the correct value * of jsonDef. + * @private */ Blockly.jsonInitFactory_ = function(jsonDef) { return function() { @@ -415,11 +416,10 @@ Blockly.jsonInitFactory_ = function(jsonDef) { /** * Define blocks from an array of JSON block definitions, as might be generated * by the Blockly Developer Tools. - * @param {!Array} jsonArray An array of JSON block definitions. + * @param {!Array.} jsonArray An array of JSON block definitions. */ Blockly.defineBlocksWithJsonArray = function(jsonArray) { - for (var index = 0; index < jsonArray.length; index++) { - var elem = jsonArray[index]; + for (var i = 0, elem; elem = jsonArray[i]; i++) { Blockly.Blocks[elem.type] = { init: Blockly.jsonInitFactory_(elem) };