Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

This is sort of a three part question. I have a JSON file. A few of the values in the JSON file are arrays. Keeping that in mind:

1) On any given page, I'd only want one set of values coming out of the JSON file. For example (as you'll see in code below) my JSON file is a list of attorneys. On any given bio page, I'd obviously only want one attorney's information. I'm currently, successfully, doing this by pulling back the entire JSON and then using ng-show. But this is causing some other issues that I'll explain in later points, so I'm wondering if there's something to put in the app.factory itself to only bring back the one set in the first place.

2) As mentioned, some of the values are arrays. This comes into play two ways in this situation. One of the ways is that there is an array of quotes about the attorney that I'll need to drop into a JS array so that my JS function can loop through them. Currently, I'm hardcoding the quotes for the one test attorney but I'm really trying to figure out how to make this dynamic. This is one reason I'm trying to figure out how to bring back only one attorney's information so I can then, somehow, say his quotes go into this array.

3) Another array value is a list of his specialty areas. I have another, hardcoded, JS object, associating the short terms with the display names. I realized though, that this has two issues.

a) The JS renders after the Angular, so I can't reference that JS in the Angular code b) I have no way , anyway, to display the JS dynamically inside the Angular code.

My solution to that aspect was to create a second JSON file holding the area hash but besides being a little cumbersome, I'm also not sure how to dynamically display just the ones I want. e.g: If my attorney only specializes in securities and litigation, how would I tell the code to only display {{areas.securities}} and {{areas.litigation}}? So,I'm open to thoughts there as well.

Here is the current, relevant code. If you need more, just ask.

Thanks.

attorneys.json (irrelevant lines removed)

{"attorneys":
[
{
    "id":1,
    "name":"Bob Smith",
    "quotes":
        [
         {
            "id": 1,
            "quote": "Wonderful guy!",
            "person": "Dovie"
         },
         {
            "id": 2,
            "quote": "If ye be wanting a haggis like no other, Bob be yer man!",
            "person": "Angus McLoed"
         },
         {
            "id": 3,
            "quote": "Wotta Hottie!",
            "person": "Bob's wife"
         }
         ],
    "areas": ["altdispute", "litigation", "securities"],
}

]
}

...and the relevant current JS object that I'm not sure what to do with:

var practiceareas = {
    altdispute: "Alternative Dispute Resolution",
    businesscorp: "Businesses & Corporations",
    estateplanning: "Estate Planning",
    futures: "Futures & Derivatives",
    litigation: "Litigation",
    productliability: "Product Liability",
    realestate: "Real Estate",
    securities: "Securities"
}

script.js (relevant function)

var idno = 0;

/* This is what I want replaced by the Angular pull */
var quotelist = ["\"Wonderful guy!\"<br/>-Dovie", "\"If ye be wanting a haggis like no other, Bob be yer man!\"<br/>-Angus McLoed", "\"Hubba, Hubba! What a hottie!\"<br/>-Bob's wife"];

$("#bio_quotes").html(quotelist[0]);

function quoteflip(id, total){

    var src1 = quotelist[idno];

    $("#bio_quotes").fadeOut(500, function(){
        $("#bio_quotes").html(src1).fadeIn(500);
    });

    idno = (id + 1) % total;

window.setTimeout(function(){quoteflip(idno, quotelist.length);}, 5000);
}

window.setTimeout(function(){quoteflip(idno, quotelist.length);}, 500);

By the way, as far as the quotes, I'm even happy to turn the JSON into a more condensed version by removing the id and consolidating the quote and author - making it an array of strings instead of mini-objects - if that makes it easier. In fact, it might be easier as far as the function anyway.

share|improve this question

1 Answer 1

  1. Can definitely filter things out at the service / factory using Array.filter. If you want to filter it server side, you have to have the code at server side that will do that. Not sure what your backend store is but definitely doable.

  2. Again, you can do this pretty easily with Array.map which let you pull specific values into a new Array. If you just want the name and quotes' quote and person name, you can definitely do this using Array .filter and .map and bind the new array to your viewmodel / scope.

  3. Hmm.. again, I'd disagree, this look like the same issue with JavaScript array manipulation. You can definitely as part of the transformation in point 1 and 2, include this so it will transfer area to the long practice area names. The easiest way to show the relevant practice area is to map it to the long name during the transformation in the service layer.

                //get matching attorney from the store by id
                var matches = data.attorneys.filter(function(a) {
                  return a.id === id;
                });
    
                //If match found,
                if (matches.length === 1) {
                  var result = matches[0];
    
                  //map the long name for practicing area
                  //use the matching attorney's area name as key
                  //and overwrite the result areas with the map
                  result.areas = result.areas.map(function(a) {
                    return practiceareas[a];
                  });
    
                  return result;
                }
    

See this solution: http://embed.plnkr.co/xBPju7/preview

As for the fade in and fade out, I'll let you figure it out...

share|improve this answer
    
I'll try this out and report back! Thanks! – Reverend Dovie Apr 7 at 5:04
    
Ok, so I finally got to dealing with this, and I must say, while I appreciate your help greatly, this was way over my head! I'm still relatively new at Angular and I was only able to understand about a third of this. :( Thank you though. – Reverend Dovie May 12 at 21:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.