how to generate the javascript code in blockly - blockly

I want to generate javascript for my blockly which does not have any input. I got the Javascript function from generator stub but it requires to assemble a javascript code into var code variable which I am not geting.
i already tried this:
var code = Blockly.JavaScript.workspaceToCode(workspace);
But it shows me code is undefined.
and I am getting this error:
Cannot read property 'call' of undefined at Blockly.Generator.blockToCode (blockly_compressed.js:1572) at Blockly.Generator.workspaceToCode (blockly_compressed.js:1570)
Can you please help me out that how do I generate javascript code and assemble it?

This is how you can generate JS code and run it:
<script>
var workspace = Blockly.inject('blocklyDiv',
{media: '../../media/',
toolbox: document.getElementById('toolbox')});
Blockly.Xml.domToWorkspace(document.getElementById('startBlocks'),
workspace);
// Generate JavaScript code and display it.
function showCode() {
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
var code = Blockly.JavaScript.workspaceToCode(workspace);
alert(code);
}
// Generate JavaScript code and run it.
function runCode() {
window.LoopTrap = 1000;
Blockly.JavaScript.INFINITE_LOOP_TRAP =
'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
var code = Blockly.JavaScript.workspaceToCode(workspace);
Blockly.JavaScript.INFINITE_LOOP_TRAP = null;
try {
eval(code);
} catch (e) {
alert(e);
}
}
</script>

Related

Using gulp for insert html in js

I have two files, js
(function (Controllers) {
var DialogElement = (function () {
function DialogElement() {
this._template = "include:'Views/Dialog.html'";
}
return DialogElement;
}());
Controllers.DialogElement = DialogElement;
})(Controllers));
and html
<div>Simple dialog!</div>
I want used gulp replaces variable this._template = "include:'Views/Dialog.html'"; on the contents of the html file.
To get so:
function DialogElement() {
this._template = "<div>Simple dialog!</div>";
}
May be there is a plugin suitable for this?
I learned gulp-html2js and gulp-inject, but they work not as expected.
So i make your salf package and and use it as:
gulp.task('inject', function () {
gulp
.src('Controllers/*.js')
.pipe(inject('Views/*.html'))
.pipe(gulp.dest('build'));
});
and chenge controller:
this._template = "{{load path='Views/Dialog.html'}}"

Defining modules in UI5

I am trying to keep my code separated in modules. When I defined my first module I extended sap.ui.base.Object and it worked. My question is: Is it a must to extend sap.ui.base.Object when defining my own module? According to the API documentation I tried following example:
sap.ui.define([], function() {
// create a new class
var SomeClass = function();
// add methods to its prototype
SomeClass.prototype.foo = function() {
return "Foo";
}
// return the class as module value
return SomeClass;
});
I required this module inside my Component.js as dependency like this:
sap.ui.define([
"path/to/SomeClass"
], function (SomeClass) {
"use strict";
//var test = new SomeClass();
I always receive a syntax error:
failed to load '[...]/Component.js' from ./Component.js: Error: failed to load '[...]/module/SomeClass.js' from ./module/Service.js: SyntaxError: Unexpected token ;
Does anyone have an idea why this happens? Thanks!
We group code in modules like this for example:
jQuery.sap.declare("our.namespace.iscool.util.Navigation");
our.namespace.iscool.util.Navigation = {
to: function (pageId, context) {
// code here
}
// etc.
}
and call the function of the module like this in a controller
jQuery.sap.require("our.namespace.iscool.util.Navigation");
sap.ui.controller("our.namespace.iscool.Detail", {
// somewhere in this file comes this
handleNavButtonPress: function (evt) {
our.namespace.iscool.util.Navigation.navBackToMaster(
evt.getSource().getBindingContext()
);
},
}
Stupid mistake - missing curly brackets in the docs.
var someclass = function() {} ;

Dynamic require in Nodejs

I'm requiring a library in NodeJS which has a self-invoking function, that results an error because it looks for an object which is not initialized at that moment .
I want to dynamically require this library when that object is initialized.
Is there any way to dynamically require/ load a library ?
This is the part of library required :
https://github.com/sakren/node-google-maps/blob/develop/lib/Google.js#L5
Actually I want to require when the window object is present (client-side rendering).
So something like this :
'use strict';
var React = require('react');
var Map = require('./map.jsx');
var Common = require('../common/common');
var MapStatic = require('./map-static.jsx');
exports.type = function() {
return 'map';
};
exports.jsx = function(data) {
if (Common.isServerSide()) {
return (<MapStatic data={data}/>);
} else {
return (
<Map data={data}/>
);
}
};
exports.transform = require('./map-transform.js');
The reason the code looks weired is that I'm using react.
In nodeJS require can be used anywhere at anytime whithout much limitations AFAIK.
Which error is thrown once you require at runtime ?
In your else branch.
Try the following.
requires = {}
function getter(key) {
if(!requires[key]){
requires[key] = require(key)
}
return requires[key]
}

What is the best way to expose methods from Node.js?

Consider I want to expose a method called Print
Binding method as prototype:
File Saved as Printer.js
var printerObj = function(isPrinted) {
this.printed = isPrinted;
}
printerObj.prototype.printNow = function(printData) {
console.log('= Print Started =');
};
module.exports = printerObj;
Then access printNow() by putting code require('Printer.js').printNow() in any external .js node program file.
Export method itself using module.exports:
File Saved as Printer2.js
var printed = false;
function printNow() {
console.log('= Print Started =');
}
module.exports.printNow = printNow;
Then access printNow() by putting code require('Printer2.js').printNow() in any external .js node program file.
Can anyone tell what is the difference and best way of doing it with respect to Node.js?
Definitely the first way. It is called the substack pattern and you can read about it on Twitter and on Mikeal Rogers' blog. Some code examples can be found at the jade github repo in the parser:
var Parser = exports = module.exports = function Parser(str, filename, options){
this.input = str;
this.lexer = new Lexer(str, options);
...
};
Parser.prototype = {
context: function(parser){
if (parser) {
this.contexts.push(parser);
} else {
return this.contexts.pop();
}
},
advance: function(){
return this.lexer.advance();
}
};
In the first example you are creating a class, ideally you should use it with "new" in your caller program:
var PrinterObj = require('Printer.js').PrinterObj;
var printer = new PrinterObj();
printer.PrintNow();
This is a good read on the subject: http://www.2ality.com/2012/01/js-inheritance-by-example.html
In the second example you are returning a function.
The difference is that you can have multiple instances of the first example (provided you use new as indicated) but only one instance of the second approach.

Node.js with external jQuery file

I'm having an issue utilizing an external jQuery file with node. I have several functions that I need to run but it isn't working. I created a simple function to test but the error message I keep getting is TypeError: Object # has no method 'alert_helloworld'. Here are my two files:
example.js
var jsdom = require('jsdom');
var templateparser = require('./templateparser.js');
var test = templateparser.alert_helloworld();
templateparser.js
function alert_helloworld(){
console.log("Hello World");
return false;
}
HELP!!!!
You need to use the exports object in templateparser.js:
exports = exports || {};
exports.alert_helloworld = function(){
console.log("Hello World");
return false;
}
Take a look at the modules docs: http://nodejs.org/docs/latest/api/modules.html
module.exports.alert_helloworld = function alert_helloworld(){
console.log("Hello World");
return false;
};
It's important you actually export the functions you need. node.js modules by default are wrapped in their own isolated module scope.

Resources