简体   繁体   English

使用Amber将选项传递给JS函数

[英]Passing options to JS function with Amber

I'm trying to write the equivalent of: 我正在尝试写相当于:

$( "#draggable" ).draggable({ axis: "y" });

in Amber smalltalk. 在Amber smalltalk。

My guess was: '#draggable' asJQuery draggable: {'axis' -> 'y'} but that's not it. 我的猜测是: '#draggable' asJQuery draggable: {'axis' -> 'y'}但不是这样。

Not working on vanilla 0.9.1, but working on master at least last two months ago is: 不适用于vanilla 0.9.1,但至少在最近两个月前在master上工作的是:

'#draggable' asJQuery draggable: #{'axis' -> 'y'}

and afaict this is the recommended way. 并且这是推荐的方式。

PS: #{ 'key' -> val. 'key2' -> val } PS: #{ 'key' -> val. 'key2' -> val } #{ 'key' -> val. 'key2' -> val } is the syntax for inline creation of HashedCollection , which is implemented (from the aforementioned two-month ago fix) so that only public (aka enumerable) properties are the HashedCollection keys. #{ 'key' -> val. 'key2' -> val }是内联创建HashedCollection的语法,它实现了(从前面提到的两个月前的修复),因此只有公共(也称为可枚举)属性是HashedCollection键。 Before the fix also all the methods were enumerable, which prevented to use it naturally in place of JavaScript objects. 在修复之前,所有方法都是可枚举的,这阻止了自然地使用它来代替JavaScript对象。

herby 's excellent answer points out the recommended way to do it. 赫尔比的优秀答案指出了推荐的方法。 Appearently, there is now Dictionary-literal support (see his comment below). 显然,现在有词典 - 文字支持(见下面的评论)。 Didn't know that :-) 不知道:-)

Old / Alternate way of doing it 做旧的/替代方式

For historical reasons, or for users not using the latest master version, this is an alternative way to do it: 由于历史原因,或者对于未使用最新主版本的用户,这是另一种方法:

options :=  <{}>. 
options at: #axis put: 'y'.
'#draggable' asJQuery draggable: options.

The first line constructs an empty JavaScript object (it's really an JSObjectProxy ). 第一行构造一个空的JavaScript对象(它实际上是一个JSObjectProxy )。

The second line puts the string "y" in the slot "axis". 第二行将字符串“y”放入槽“轴”中。 It has the same effect as: 它具有与以下相同的效果:

options.axis = "y"; // JavaScript

Lastly, it is invoked, and passed as a parameter. 最后,它被调用,并作为参数传递。

Array-literals vs Dictionaries 数组文字与字典

What you were doing didn't work because in modern Smalltalk (Pharo/Squeak/Amber) the curly-brackets are used for array-literals, not as an object-literal as they are used in JavaScript. 你正在做的事情没有用,因为在现代的Smalltalk(Pharo / Squeak / Amber)中,花括号用于数组文字,而不是像JavaScript中使用的对象文字。

If you evaluate (print-it) this in a Workspace: 如果您在工作区中评估(打印)它:

{ #elelemt1. #element2. #element3 }.

You get: 你得到:

a Array (#elelemt1 #element2 #element3)

As a result, if you have something that looks like a JavaScript object-literal in reality it is an Array of Association(s). 因此,如果你有一些看起来像JavaScript对象的东西 - 实际上它是一个关联数组。 To illustrate I give you this snippet, with the results of print-it on the right: 为了说明我给你这个片段,右边是print-it的结果:

arrayLookingLikeObject := { #key1 -> #value1. #key2 -> #value2. #key3 -> #value3}.

arrayLookingLikeObject class. "==> Array"
arrayLookingLikeObject first class. "==> Association"
arrayLookingLikeObject "==> a Array (a Association a Association a Association)" 

I wrote about it here: 我在这里写到:

http://smalltalkreloaded.blogspot.co.at/2012/04/javascript-objects-back-and-forth.html http://smalltalkreloaded.blogspot.co.at/2012/04/javascript-objects-back-and-forth.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM