gravicappa / shen-js

Shen on javascript.
Other
68 stars 12 forks source link

Documentation to call javascript from shen #7

Closed optevo closed 11 years ago

optevo commented 11 years ago

HI - in the documentation,would it be possible to show how to call javascript from shen with an example?

gravicappa commented 11 years ago

Hi - in the documentation,would it be possible to show how to call javascript from shen with an example?

Hello. It is done like this.

Create a file containing bindings to js-functions (let's call it web.js):

// Shen.defun(function_name, number_of_arguments, js_func)

Shen.defun("web.get-element-by-id", 1, function(args) {
  return document.getElementById(args[0])
})

Shen.defun("web.create-element", 1, function(args) {
  return document.createElement(args[0])
}

Shen.defun("web.create-text-node", 1, function(args) {
  return document.createTextNode(args[0])
}

Shen.defun("web.append-child", 2, function(args) {
  return args[0].appencChild(args[1])
})

Then load this file either by

(shenjs.load "web.js")

Or if you run Shen in a browser you can load it using standard methods ( Githubissues.

  • Githubissues is a development platform for aggregating issues.