开发者

How to access an array returned from a lift call

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(&quot;F306228675550KFT=true&quot;, 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(&quot;F306228675550KFT=true&quot;, null, null, null); return false;\">delete</button>"]
                                 ,0)[0]).id = "2";

How does one

  1. Get the 0th element from the returned array?
  2. 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(&quot;F306228675550KFT=true&quot;, null, null, null); return false;\">delete</button>"]
                   ,0)[0]).id = 2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜