2016-06-20 166 views
2

我有我們的工作人員在一個JSON文件中,並有想到與ES6類使用該數據的想法。我對此的工作越多,我越覺得自己可能錯過了一些東西。我在這的CoffeeScript工作這樣:將JSON映射到ES6類

fetch = require('node-fetch') 
domain = 'domain.com' 
apiSrc = 'api.domain.com' 
slug = 'people' 

class Person 
    constructor: (json) -> {name: @name, title: @title, school: @school, bio: @bio} = json 
    email: (username) -> 
    username.replace(/\s/, '.').toLowerCase() + '@' + domain 
    profile: -> 
    content = [] 
    if this.name then content.push("#{@name}") 
    if this.title then content.push("#{@title}") 
    if this.school then content.push(school) for school in "#{@school}" 
    if this.bio then content.push("#{@bio}") 
    if this.name then content.push(this.email("#{@name}")) 
    content.join('') 

fetch('http://' + apiSrc + '/' + slug + '.json') 
    .then((response) -> response.json()) 
    .then((api) -> 
    content = [] 
    group = [] 
    group.push(new Person(them)) for them in api[slug] 
    for them, index in group 
     content.push(them.profile()) 
     console.log(content.join('')) 
) 

但我認爲這將是更好的,如果我可以把它轉換爲ES6。我知道用例很簡單,並且類肯定不是必需的,因爲我只是使用數據進行模板化,但是爲了學習,我試圖做到這一點。我是否以這種錯誤的方式去做?現在,我覺得應該有辦法讓我把所有的「人」放回Person課。但是,我可以想出如何做到這一點的唯一方法是運行for循環,然後將其寫入文檔。

class Person { 
    constructor(data) { ({name: this.name, title: this.title, school: this.school, bio: this.bio, email: email(this.name)} = data); } 
    email(username) { 
    return username.replace(/\s/, '.').toLowerCase() + '@' + location.hostname.replace(/[^\.\/\@]+\.[^\.\/]+$/, ''); 
    } 
    profile() { 
    return `${this.name} ${this.title}`; 
    } 
} 

var apiSrc = 'api.domain.com'; 
var slug = 'people'; 
fetch(`http://${apiSrc}/${slug}.json`) 
    .then(function(response) { return response.json() }) // .then(response => response.json()) 
    .then(function(api) { 
    var content = []; 
    var group = []; 
    for (var i = 0; i < api[slug].length; i++) { var them = api[slug][i]; new Person(them); } 
    for (var i = 0; index < group.length; i++) { 
     var them = group[i]; 
     content.push(them.profile()); 
     console.log(content.join('')); 
    } 
    }); 

我的ES6轉換實際上現在還沒有工作。 API返回JSON,但之後會變得混亂。任何建議都會非常有用,因爲我試圖讓自己更好地成爲一名編碼人員,希望這種例子能夠幫助其他人在真實用例中學習類。

+0

我不明白你的意思「*我覺得應該是返回所有的方式‘人’,我投入了'Person'類*」 。你做得很好,有(也應該是)無法收集所有類的實例。你可以返回你放在'group'數組中的所有人。這正是它應該如何工作。 – Bergi

+0

因爲我沒有類的經驗,所以我認爲有某種類型的返回(像這樣做)Object.getClassEntries(allthePeople.constructor())。當我返回'group'並且它工作時,我注意到構造函數似乎對輸出沒有任何影響。所以我認爲如果構造函數沒有做任何事情,我必須做錯事。 – BarryMode

+0

我不確定你的預期會有什麼影響。你的ES6類與你的CS類非常相似(不考慮'profile'返回值)。你期望的行爲有什麼不同? – Bergi

回答

5

您可以嘗試使用JSON.parse的參數reviver。下面是一個簡單的例子:

class Person { 
    // Destructure the JSON object into the parameters with defaults 
    constructor ({name, title, school=[]}) { 
    this.name = name 
    this.title = title 
    this.school = school 
    } 
} 

var api = JSON.parse(a, function (k,v) { 
    // Is this a new object being pushed into the top-level array? 
    if (Array.isArray(this) && v.name) { 
    return new Person(v) 
    } 
    return v 
}) 

var group = api["people/administration"] 
+0

謝謝,起初我以爲這不起作用,因爲我的瀏覽器有一些兼容性問題,你大多隻是給了我一個開始的地方。然而,過了一段時間後,我能夠實現這個總體思路,從我以前的兩個循環中節省了大量的迭代。 – BarryMode