I am trying to generate some Javascript for a Comet callback. The code that I have works, but needs to use several additional variables that should not really be required. The problem is that I am not sure how to access an element in an array that is returned from a Call.
JsCrVar("node" + c.id, Call("dataTable.fnAddData",
JsArray(Text(c.name),
Text(c.initials),
makeDeleteButton(c)),
Num(0))) &
JsCrVar("row" + c.id, Call("dataTable.fnGetNodes", JsVar("node" + c.id + "[0]"))) &
SetExp(JsVar("row" + c.id + ".id"), Str(c.id.toString))
This generates JavaScript like follows (indented for readability):
var node2 = dataTable.fnAddData(["Test User",
"TU",
"<button onclick=\"liftAjax.lift_ajaxHandler("F306228675550KFT=true", null, null, null); return false;\">delete</button>"]
,0);
var 开发者_StackOverflow社区row2 = dataTable.fnGetNodes(node2[0]);
row2.id = "2";
The code that I would like to generate is as follows:
dataTable.fnGetNodes(dataTable.fnAddData(["Test User",
"TU",
"<button onclick=\"liftAjax.lift_ajaxHandler("F306228675550KFT=true", null, null, null); return false;\">delete</button>"]
,0)[0]).id = "2";
How does one
- Get the
0
th element from the returned array? - Get the sub-element 'id' from that returned object?
I think you'll have to create your own custom class to return the n'th member of an array that's returned from Call. Try something like this:
case class JsRetArray(array: JsExp, n: Int) extends JsExp {
def toJsCmd = array.toJsCmd + "[" + n + "]"
}
Then you can do:
Call("dataTable.fnGetNodes",
JsRetArray(Call("dataTable.fnAddData",
JsArray(Text(c.name),
Text(c.initials),
makeDeleteButton(c)),
Num(0)),
0)
) ~> Id === 2
which, when called with a .toJsCmd
, yields:
dataTable.fnGetNodes(dataTable.fnAddData(["Test User",
"TU",
"<button onclick=\"liftAjax.lift_ajaxHandler("F306228675550KFT=true", null, null, null); return false;\">delete</button>"]
,0)[0]).id = 2
精彩评论