Skip to content

Commit ebe8ada

Browse files
committed
use backbone models from nashorn
1 parent 7172f3d commit ebe8ada

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

res/nashorn6.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
load('http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js');
2+
load('http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js');
3+
4+
5+
// simple backbone model:
6+
// valueOfGoods will automatically be calculated when stock or price changes
7+
var Product = Backbone.Model.extend({
8+
defaults: {
9+
stock: 0,
10+
price: 0.0,
11+
name:'',
12+
valueOfGoods: 0.0
13+
},
14+
15+
initialize: function() {
16+
this.on('change:stock change:price', function() {
17+
var stock = this.get('stock');
18+
var price = this.get('price');
19+
var valueOfGoods = this.getValueOfGoods(stock, price);
20+
this.set('valueOfGoods', valueOfGoods);
21+
});
22+
},
23+
24+
getValueOfGoods: function(stock, price) {
25+
return stock * price;
26+
}
27+
});
28+
29+
var product = new Product();
30+
product.set('name', 'Pencil');
31+
product.set('stock', 1000);
32+
product.set('price', 3.99);
33+
34+
35+
// pass backbone model to java method
36+
var Nashorn6 = Java.type('com.winterbe.java8.Nashorn6');
37+
Nashorn6.getProduct(product.attributes);
38+
39+
40+
// bind java object to backbone model and pass result back to java
41+
var calculate = function(javaProduct) {
42+
var model = new Product();
43+
model.set('name', javaProduct.name);
44+
model.set('price', javaProduct.price);
45+
model.set('stock', javaProduct.stock);
46+
return model.attributes;
47+
};

src/com/winterbe/java8/Nashorn6.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.winterbe.java8;
2+
3+
import jdk.nashorn.api.scripting.ScriptObjectMirror;
4+
5+
import javax.script.Invocable;
6+
import javax.script.ScriptEngine;
7+
import javax.script.ScriptEngineManager;
8+
9+
/**
10+
* Using Backbone Models from Nashorn.
11+
*
12+
* @author Benjamin Winterberg
13+
*/
14+
public class Nashorn6 {
15+
16+
public static void main(String[] args) throws Exception {
17+
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
18+
engine.eval("load('res/nashorn6.js')");
19+
20+
Invocable invocable = (Invocable) engine;
21+
22+
Product product = new Product();
23+
product.setName("Rubber");
24+
product.setPrice(1.99);
25+
product.setStock(1337);
26+
27+
ScriptObjectMirror result = (ScriptObjectMirror)
28+
invocable.invokeFunction("calculate", product);
29+
System.out.println(result.get("name") + ": " + result.get("valueOfGoods"));
30+
}
31+
32+
public static void getProduct(ScriptObjectMirror result) {
33+
System.out.println(result.get("name") + ": " + result.get("valueOfGoods"));
34+
}
35+
36+
}

src/com/winterbe/java8/Product.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ public class Product {
77
private String name;
88
private double price;
99
private int stock;
10+
private double valueOfGoods;
11+
12+
public double getValueOfGoods() {
13+
return valueOfGoods;
14+
}
15+
16+
public void setValueOfGoods(double valueOfGoods) {
17+
this.valueOfGoods = valueOfGoods;
18+
}
1019

1120
public String getName() {
1221
return name;

0 commit comments

Comments
 (0)