2016-11-02 22 views
0

可能這是一個不好的問題。目前我在擺弄Framer.js。我有一個CoffeeScript問題;在循環中聲明動態變量Coffescript

types = ["orange", "apple", "banana", "grapefruit", "pear"] 
for i in types 
    li = new TextLayer 
     text: types 
     y: li * 60 
    li.parent = dropdownList 

    print "list-item-" + "#{i}", i 

所以我有一個數組,我想聲明一個動態變量的對象實例。上面的代碼只生成5個li圖層(這是Framer特定的>我不想在編輯器中的非自解釋圖層名稱)

因此,在for-loop;

VAR項橙色=新層...

VAR項蘋果=新層... 等

我怎麼能做到這一點跟CoffeeScript的?

+0

什麼「編輯器」你是什麼意思?你爲什麼不使用一個對象('{orange:...,apple:...}')呢? –

+0

你想完成什麼? 'y:li * 60'是什麼意思,這不是NaN嗎?爲什麼將整個數組類型分配爲文本?這份印刷聲明有什麼意義?你能用預期和實際產出來解釋你的問題嗎? – kba

+0

也許我應該剝去Framer-lingo的其餘部分。基本上,數組中的每個值都將以垂直60px的間距添加到父dropdownList中。打印功能是在Framer Studio中執行console.log的一種方法。我在做一些擺弄。用「#{i}」是獲取元素值的一種方法。 Framer.js是一個帶有名爲Framer Studio的IDE的原型框架。 – myradon

回答

0

我不確定,但我認爲你要做的是按名稱獲取每個創建圖層的引用,對吧?你可以通過在對象存儲他們的名字的標記下做到這一點:

types = ["orange", "apple", "banana", "grapefruit", "pear"] 

# Create an empty layers object, outside of the loop 
layers = {} 

# Loop over the types array, storing the type in the variable named type 
# And the number of the current loop in the variable index 
for type, index in types 
    li = new Layer 
     html: type 
     y: index * 220 
     name: "list-item-#{type}" 
    # Store the layer in the layer object under the type key 
    layers[type] = li 

    print "list-item-" + "#{type}", layers[type] 

# Get a specific layer out of the layers array 
layers['apple'].animate 
    x: 300 

完整的示例是在這裏:http://share.framerjs.com/5owxrbz5hqse/

+0

方便的例子。在你的例子中,你會在Framer Studio中看到每個實例都被命名爲li。我希望將它們看作圖層列表項目橙色,圖層列表項目蘋果等等。 – myradon

+0

您可以通過使用'name:'屬性顯式地命名圖層來實現這一點,我已經更新了示例以包含此圖層。 – Niels

+0

Niels非常感謝! – myradon