Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitebe8ada

Browse files
committed
use backbone models from nashorn
1 parent7172f3d commitebe8ada

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+
varProduct=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+
varstock=this.get('stock');
18+
varprice=this.get('price');
19+
varvalueOfGoods=this.getValueOfGoods(stock,price);
20+
this.set('valueOfGoods',valueOfGoods);
21+
});
22+
},
23+
24+
getValueOfGoods:function(stock,price){
25+
returnstock*price;
26+
}
27+
});
28+
29+
varproduct=newProduct();
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+
varNashorn6=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+
varcalculate=function(javaProduct){
42+
varmodel=newProduct();
43+
model.set('name',javaProduct.name);
44+
model.set('price',javaProduct.price);
45+
model.set('stock',javaProduct.stock);
46+
returnmodel.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+
packagecom.winterbe.java8;
2+
3+
importjdk.nashorn.api.scripting.ScriptObjectMirror;
4+
5+
importjavax.script.Invocable;
6+
importjavax.script.ScriptEngine;
7+
importjavax.script.ScriptEngineManager;
8+
9+
/**
10+
* Using Backbone Models from Nashorn.
11+
*
12+
* @author Benjamin Winterberg
13+
*/
14+
publicclassNashorn6 {
15+
16+
publicstaticvoidmain(String[]args)throwsException {
17+
ScriptEngineengine =newScriptEngineManager().getEngineByName("nashorn");
18+
engine.eval("load('res/nashorn6.js')");
19+
20+
Invocableinvocable = (Invocable)engine;
21+
22+
Productproduct =newProduct();
23+
product.setName("Rubber");
24+
product.setPrice(1.99);
25+
product.setStock(1337);
26+
27+
ScriptObjectMirrorresult = (ScriptObjectMirror)
28+
invocable.invokeFunction("calculate",product);
29+
System.out.println(result.get("name") +": " +result.get("valueOfGoods"));
30+
}
31+
32+
publicstaticvoidgetProduct(ScriptObjectMirrorresult) {
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
privateStringname;
88
privatedoubleprice;
99
privateintstock;
10+
privatedoublevalueOfGoods;
11+
12+
publicdoublegetValueOfGoods() {
13+
returnvalueOfGoods;
14+
}
15+
16+
publicvoidsetValueOfGoods(doublevalueOfGoods) {
17+
this.valueOfGoods =valueOfGoods;
18+
}
1019

1120
publicStringgetName() {
1221
returnname;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp