Created
April 1, 2014 18:03
-
-
Save winterbe/9919583 to your computer and use it in GitHub Desktop.
Using Backbone Models from Nashorn JS Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.winterbe.java8; | |
import jdk.nashorn.api.scripting.ScriptObjectMirror; | |
import javax.script.Invocable; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
/** | |
* Using Backbone Models from Nashorn. | |
* | |
* @author Benjamin Winterberg | |
*/ | |
public class Nashorn6 { | |
public static void main(String[] args) throws Exception { | |
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); | |
engine.eval("load('res/nashorn6.js')"); | |
Invocable invocable = (Invocable) engine; | |
Product product = new Product(); | |
product.setName("Rubber"); | |
product.setPrice(1.99); | |
product.setStock(1337); | |
ScriptObjectMirror result = (ScriptObjectMirror) | |
invocable.invokeFunction("calculate", product); | |
System.out.println(result.get("name") + ": " + result.get("valueOfGoods")); | |
} | |
public static void getProduct(ScriptObjectMirror result) { | |
System.out.println(result.get("name") + ": " + result.get("valueOfGoods")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load('http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js'); | |
load('http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js'); | |
// simple backbone model: | |
// valueOfGoods will automatically be calculated when stock or price changes | |
var Product = Backbone.Model.extend({ | |
defaults: { | |
stock: 0, | |
price: 0.0, | |
name:'', | |
valueOfGoods: 0.0 | |
}, | |
initialize: function() { | |
this.on('change:stock change:price', function() { | |
var stock = this.get('stock'); | |
var price = this.get('price'); | |
var valueOfGoods = this.getValueOfGoods(stock, price); | |
this.set('valueOfGoods', valueOfGoods); | |
}); | |
}, | |
getValueOfGoods: function(stock, price) { | |
return stock * price; | |
} | |
}); | |
var product = new Product(); | |
product.set('name', 'Pencil'); | |
product.set('stock', 1000); | |
product.set('price', 3.99); | |
// pass backbone model to java method | |
var Nashorn6 = Java.type('com.winterbe.java8.Nashorn6'); | |
Nashorn6.getProduct(product.attributes); | |
// bind java object to backbone model and pass result back to java | |
var calculate = function(javaProduct) { | |
var model = new Product(); | |
model.set('name', javaProduct.name); | |
model.set('price', javaProduct.price); | |
model.set('stock', javaProduct.stock); | |
return model.attributes; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment