1

I am new to Aurelia and have gone thru the tutorials on PluralSight and Egghead.io but still can't figure out my issue.

I have a json file, let's call it bob.json. Inside has a collection of objects about bob. Each object has an id. I want to use arrow notation to get an object in the collection by id. Here is what I've done so far.

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);
        });
    }
}

The ellipsis parts are where I have no clue what to put for the arrow function. My questions are, can someone help me understand what needs to be placed in those areas to return just the object that matches that specific id and also where is the best resource that explains how to use arrow notation with json data.

UPDATE:

So I found a good explanation on:

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

From what I can gather it's like an SQL statement in a way.

in bodyData.js I used this statement:

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

Now, how I understand that is filter the returned content where the objects id equals the passed in id. Kind of how SQL would read:

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

if this is correct then I think I got that part. The part that is really throwing me now is the displayPart(e) function in the person.js file. How do I view this data to see if it is returning the correct value? Any help is greatly appreciated.

C. Daniel
  • 141
  • 14
  • I'm not sure about what did you mean with "arrow notation". I believe you are looking for "arrow functions". To learn more about arrow functions take a look at https://babeljs.io/docs/learn-es2015/#arrows-and-lexical-this. When you use `this.http.get`, you are making an http request. Usually, you should have an specific http uri that returns only the selected record (by Id in your case). If you want to search the record at object level (from an array, for example), you should use an `array.filter()` method instead of `this.http.get` – Fabio Jan 22 '16 at 14:04
  • I am using .get because in the end it will pull from a an api. Using the .get still gets the json data from ths file and it works. When I am using an API what would be used to return the specific id from the collection? – C. Daniel Jan 22 '16 at 14:09
  • "arrow functions" proper name is a [lambda expression](https://en.wikipedia.org/wiki/Lambda_calculus) – Liam Jan 22 '16 at 14:21
  • this should help a little. [What is Lambda expression?](http://stackoverflow.com/questions/3832337/interview-what-is-lambda-expression) I don't unfortunately know the framework, so I'm not clear what `then` passes into the function, or I'd help more. – Liam Jan 22 '16 at 14:28
  • They're derived from C#, JavaScript has only recently started to use them. The [C# reference for these](https://msdn.microsoft.com/en-us/library/bb397687.aspx) may also help. – Liam Jan 22 '16 at 14:31
  • @Liam, I updated the file with what I found for lambda but the displayPart(e) function has me stumped. How would I console out the data returned just to show I have something? – C. Daniel Jan 22 '16 at 14:54
  • Why don't you [attach a debugger](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) then you can examine all the objects? – Liam Jan 22 '16 at 15:06
  • @Liam, I get back the Promise. It contains the correct info so I know the updated code works but how do I get back just the object and not the entire promise? I tried adding a .then(bp => this.bp = bp); but it still just returns the entire promise. – C. Daniel Jan 22 '16 at 15:29
  • this.bodyData.getBodyPart(e.target.id).then((res) => { // use you result here }) – valichek Jan 22 '16 at 16:25

2 Answers2

2

EUREKA! I did it! Okay, let me calm down as many of you probably think this is small but it's big to me. So here is the final piece. after the update I did above, you have to do all your next steps INSIDE the promises THEN statement.

So, replace the displayPart(e) code in my example in the person.js class with the following:

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.
    });
}

This returns a collection of the objects that match the id I sent in. WOOT! Problem solved. Hope this helps someone someday.

C. Daniel
  • 141
  • 14
-2

About arrow notations:

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

so you need:

var pairs = evens.map(v => ({even: v, odd: v + 1}));
valichek
  • 1,186
  • 6
  • 9
  • Arrow notation == [lambda expression](https://en.wikipedia.org/wiki/Lambda_calculus) – Liam Jan 22 '16 at 14:22
  • this appears to be answering a different question. The OP asked *My questions are, can someone help me understand what needs to be placed in those areas to return just the object that matches that specific id*. He does ask for how lambda's work but the main question is above – Liam Jan 22 '16 at 14:25