开发者

How to access an array via RJS proxy?

In Rails' RJS Adapter,

page['id'] // $('id')

accesses an CSS-id,

page['id'].property // $('id').property

a property of it. But how can I access an array index, e.g.

page.select('ul').value_a开发者_如何学编程t(2) // $('id').select('ul')[2]

Is there any way of doing this without writing:

page << "$('id').select('ul')[2]"


You can't access arrays like that from rjs but, anyone of these would work:

page['id']['firstChild']['nextSibling']
page['id'].down(2)

Generates:

$("id").firstChild.nextSibling;
$("id").down(2);

Of course, the best solution would be to add some identifying css class or element id to the second element and select by that from your rjs file.

If you'd like to stick to using page.select, you could implement value_at like this (called pick here):

>>> Array.prototype.pick = function(n) { return this[n]; };
function()
>>> ['a', 'b', 'c'].pick(1)
"b"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜