HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Storing get call data on same function

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
samestoringfunctioncallgetdata

Problem

I basically have a folder view structure, that each time I call grabs the value of the selected field and gets that specific folder data. I was wondering if there's a better way to do this by caching or something like that. So for example if I was to request that same folder it would be stored in memory, instead of having to make another get request.

```
var getFolder = function() {

if ($('.list-group-item:hover').css('margin-left') === '0px') {
$('.fold').remove();
}

var promises = [];

$('.list-group-item').css("background-color", ""); // clear last selected

$.get("/folder", {
folder: $('.list-group-item:hover').attr('title') //.clone().children().text()
}, function(data) {
var folders = data.CommonPrefixes; // folders
var jsonData = data.Contents; //json data

var selected = $('.list-group-item:hover').css("background-color", "rgb(0, 196, 255)"); // new selected color

var parFolder = parseInt($('.list-group-item:hover').css('margin-left'), 10);
var add = parFolder + 15;
var subFolder = add.toString() + 'px';

if (folders.length > 0) {

for (var k = 0; k " + "" + displayVal + "" + ""

$(book).insertAfter(".list-group-item:hover").css('margin-left', subFolder);

}
}

if (jsonData.length > 0) {
for (var i = 0; i ' +
'' +
'' +
'' +
'' + title + '' +
'' +
'' + description + '' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'' +
'');
}

if (!fileBody.hasOwnProperty('position')) {
$(".static").append('' +

Solution

var selected = $('.list-group-item:hover').css("background-color", "rgb(0, 196, 255)");


rgb(0, 196, 255 tells me nothing, as I am not a computer and I can't visualize a color with just those numbers in my head.

Create a variable called with the name of the equivalent color in all caps, and set it to that rgb string.

For example, if you were changing the background to red, you would put:

var REDISH_ORANGE = "rgb(255, 68, 0)";

var selected = $('.list-group-item:hover').css("background-color", REDISH_ORANGE);


var parFolder = parseInt($('.list-group-item:hover').css('margin-left'), 10);
var add = parFolder + 15;
var subFolder = add.toString() + 'px';


This is a waste of memory; you are only using parFolder and add a single time, and that single time is to help create the variable right beneath it.

Just chain these together and stick them in subFolder:

var subFolder = (parseInt($('.list-group-item:hover').css('margin-left', 10) + 15).toString() + 'px';


Yes, you are only using subFolder once, but the name "subFolder" is describing what the value is. Names like "parFolder" (which I assume is short for "Parsed int folder", or something like that) and "add" sound like "filler words" and mean nothing to the variable or context.

You use

$('.list-group-item:hover')


quite a lot. Stick this into a variable so it is easier to access.

In your function, you should "use strict";.

This code was a little difficult to review without seeing the webpage, and without seeing the file structure/tree.

If you could add these to your post, I could edit my answer to add more improvements.

Code Snippets

var selected = $('.list-group-item:hover').css("background-color", "rgb(0, 196, 255)");
var REDISH_ORANGE = "rgb(255, 68, 0)";

var selected = $('.list-group-item:hover').css("background-color", REDISH_ORANGE);
var parFolder = parseInt($('.list-group-item:hover').css('margin-left'), 10);
var add = parFolder + 15;
var subFolder = add.toString() + 'px';
var subFolder = (parseInt($('.list-group-item:hover').css('margin-left', 10) + 15).toString() + 'px';
$('.list-group-item:hover')

Context

StackExchange Code Review Q#94996, answer score: 3

Revisions (0)

No revisions yet.