2013-05-11 69 views
0

這應該打印出集合 中的所有內容,但它只打印出兩個元素。在構造函數中打印數組coffeescript

爲什麼不打印出整個列表? 這是一種比賽案例嗎?

http://jsfiddle.net/Czenu/1/

class window.Restful 
    constructor:-> 
    _.each @collection, (action,kind)=> 
     $('.actions').append "<div>#{action} #{kind}</div>" 

class Material extends Restful 
    namespace: 'admin/api' 
    table_name: 'materials' 
    constructor:(@$rootScope,@$http)-> 
    super 
    collection: 
    get: 'downloaded' 
    get: 'incomplete' 
    get: 'submitted' 
    get: 'marked' 
    get: 'reviewed' 
    get: 'corrected' 
    get: 'completed' 
    post: 'sort' 
    post: 'sort_recieve' 

new Material() 

回答

2

collection對象由只用兩個不同的密鑰元素: 「get」 和 「後」。由於每個鍵只能映射到一個值,您的目的是簡化爲:

collection: 
 get: 'downloaded' ... get: 'corrected' 
get: 'completed' 
 post: 'sort' 
post: 'sort_recieve' 

的解決方案是使更senseful目的,例如自定義對象的數組(使用快捷功能與適當的變換名稱創建如下例所示)。

class window.Restful 
    constructor: -> 
    _.each @collection, (obj) => 
     {action,kind} = obj 
     $('.actions').append "<div>#{action} #{kind}</div>" 

class Material extends Restful 
    get = (action) -> {action, kind:'get'} 
    post = (action) -> {action, kind:'post'} 
    ... 

    collection: [ 
    get 'downloaded' 
    get 'incomplete' 
    get 'submitted' 
    get 'marked' 
    get 'reviewed' 
    get 'corrected' 
    get 'completed' 
    post 'sort' 
    post 'sort_recieve' 
    ] 

完整的結果顯示在http://jsfiddle.net/Czenu/2/