带有 json 文件的 Aurelia 上的箭头函数

Arrow function on Aurelia with a json file

我是 Aurelia 的新手,已经学习了 PluralSight 和 Egghead.io 上的教程,但仍然无法解决我的问题。

我有一个 json 文件,我们称它为 bob.json。里面有一组关于鲍勃的对象。每个对象都有一个 id。我想使用箭头表示法通过 id 获取集合中的对象。这是我到目前为止所做的。

bob.json

[
    {
        "id": 1,
        "name": "eyes",
        "position": "head",
        "color": "blue"
    },
    {
        "id": 2,
        "name": "nose",
        "position": "head",
        "shape": "triangle"
    },
    {
        "id": 3,
        "name": "mouth",
        "position": "head",
        "chapped": true
    },
    [more body data here]
]

person.html

<template>
    <ul repeat.for="bodypart of bodyparts">
        <li>
            <a id="${bodypart.id}" href="javascript:void(0);" click.trigger="displayPart($event)">${bodypart.name}</a>
        </li>
    </ul>
</template>

person.js

import {inject} from "aurelia-framework";
import {BodyData} from "bodyData";

@inject(BodyData)
export class(bodyData) {
    constructor(bodyData){
        this.bodyData = bodyData;
    }

    activate(){
        return this.bodyData.getAll().then(bodyparts => this.bodyparts = bodyparts)
    }

    displayPart(e){
        this.bodyData.getBodyPart(e.target.id)...
    }
}

bodyData.js

import {inject} from "aurelia-framework";
import {httpClient} from "aurelia-http-client";

let baseUrl = "bob.json";

@inject(httpClient)
export class BodyData {
    constructor(httpClient){
        this.http = httpClient;
    }

    getAll(){
        return this.http.get(baseUrl).then(response => { return response.content });
    }

    getBodyPart(id){
        return this.http.get(baseUrl).then(response => {
            return response.content.filter(n => n.id == id);
        });
    }
}

省略号部分是我不知道为箭头函数添加什么的地方。我的问题是,有人可以帮助我理解需要在这些区域中放置什么 return 只是匹配该特定 id 的对象以及解释如何使用箭头符号的最佳资源在哪里 json数据。

更新:

所以我找到了一个很好的解释:

https://googlechrome.github.io/samples/arrows-es6/

据我所知,它在某种程度上类似于 SQL 声明。

在bodyData.js中我使用了这个语句:

getBodyPart(id) {
    return this.http.get(baseUrl).then(response => {
        return response.content.filter(n => n.id == id);
    });
}

现在,我的理解是过滤对象 ID 等于传入 ID 的 returned 内容。 SQL 的读法:

SELECT [object]
FROM [json file]
WHERE [object].id = id

如果这是正确的,那么我想我明白了。现在真正让我感到困惑的部分是 person.js 文件中的 displayPart(e) 函数。我如何查看此数据以查看它 return 是否是正确的值?非常感谢任何帮助。

关于箭头符号:

https://github.com/lukehoban/es6features#arrows

所以你需要:

var pairs = evens.map(v => ({even: v, odd: v + 1}));

尤里卡!我做到了!好吧,让我冷静下来,因为你们中的许多人可能认为这很小,但对我来说却很重要。所以这是最后一块。在我完成上述更新后,您必须在 promises THEN 语句中执行所有后续步骤。

因此,将 person.js class 示例中的 displayPart(e) 代码替换为以下代码:

displayPart(e){
    this.bodyData.getBodyPart(e.target.id).then(bp => {
        this.bp = bp;
        // Whatever code/functionality you plan to do with the body part next by using this.bp to access the collection.
    });
}

这个 returns 与我发送的 ID 相匹配的对象集合。太棒了!问题解决了。希望有一天这对某人有所帮助。