Create a separate JSON + syntax
$begingroup$
My code already works perfectly. Here is the JS file:
var movieData = {
count: 6,
movies: [{
id: 1,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 2,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 3,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 4,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 5,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 6,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
]
};
$(document).ready(function() {
if (typeof movieData === 'object' && typeof movieData !== null) {
// List the movies
for (var i in movieData.movies) {
var movie = movieData.movies[i];
var movieDiv =
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
$('#films').append(movieDiv);
}
}
});
I actually have everything into one file. As a code review I am asking your help if you have any idea about how to improve the code.
Knowing that as an improvement I already though about making this code into 2 different files (one JS and one JSON) but I would like to know your point of view.
javascript json
New contributor
$endgroup$
add a comment |
$begingroup$
My code already works perfectly. Here is the JS file:
var movieData = {
count: 6,
movies: [{
id: 1,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 2,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 3,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 4,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 5,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 6,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
]
};
$(document).ready(function() {
if (typeof movieData === 'object' && typeof movieData !== null) {
// List the movies
for (var i in movieData.movies) {
var movie = movieData.movies[i];
var movieDiv =
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
$('#films').append(movieDiv);
}
}
});
I actually have everything into one file. As a code review I am asking your help if you have any idea about how to improve the code.
Knowing that as an improvement I already though about making this code into 2 different files (one JS and one JSON) but I would like to know your point of view.
javascript json
New contributor
$endgroup$
add a comment |
$begingroup$
My code already works perfectly. Here is the JS file:
var movieData = {
count: 6,
movies: [{
id: 1,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 2,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 3,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 4,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 5,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 6,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
]
};
$(document).ready(function() {
if (typeof movieData === 'object' && typeof movieData !== null) {
// List the movies
for (var i in movieData.movies) {
var movie = movieData.movies[i];
var movieDiv =
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
$('#films').append(movieDiv);
}
}
});
I actually have everything into one file. As a code review I am asking your help if you have any idea about how to improve the code.
Knowing that as an improvement I already though about making this code into 2 different files (one JS and one JSON) but I would like to know your point of view.
javascript json
New contributor
$endgroup$
My code already works perfectly. Here is the JS file:
var movieData = {
count: 6,
movies: [{
id: 1,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 2,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 3,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 4,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 5,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
{
id: 6,
title: "Lorem Ipsum",
thumb: "assets/img/placehold.png"
},
]
};
$(document).ready(function() {
if (typeof movieData === 'object' && typeof movieData !== null) {
// List the movies
for (var i in movieData.movies) {
var movie = movieData.movies[i];
var movieDiv =
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
$('#films').append(movieDiv);
}
}
});
I actually have everything into one file. As a code review I am asking your help if you have any idea about how to improve the code.
Knowing that as an improvement I already though about making this code into 2 different files (one JS and one JSON) but I would like to know your point of view.
javascript json
javascript json
New contributor
New contributor
edited 19 mins ago
Jamal♦
30.3k11117227
30.3k11117227
New contributor
asked yesterday
CedCed
1114
1114
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
I would recommend removing count
if all it does is represent the number of items in movies
. Otherwise, you'll have 2 sources of truth in the data (count
and the length
of movies
) and code that takes sides down the line (one that believes in count
and one that believes in the length of movies
). So as much as possible, avoid duplicating data.
Now it's not always feasible to recompute data all the time (i.e. if you had to filter thousands of rows to count "selected" items). This is one case where I'd put a fixed number instead of recomputing it from some property. Just make sure it's documented somewhere.
for (var i in movieData.movies) {
Use regular for
loops when looping through arrays. The problem with for-in
is that it runs through all enumerable properties, indices and other enumerable properties. You might be getting more than what's just in the array. An even better suggestion is to use array.forEach
.
$('#films').append(movieDiv);
The problem with this approach is that you're doing a jquery.append
on every item. That means the browser has to re-render every time you add an item. What I suggest is to create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. This way, the browser only ever renders once.
// Creates a that's in memory, not yet appended to the DOM.
const replacementFilms = $('', { id: '#films' })
movieData.movies.forEach(movie => {
var movieDiv = '...the markup...'
// Appends to our that's "in memory".
replacementFilms.append(movieDiv)
})
// Replace the #films in the DOM with the one in memory in one go.
In this example, I replaced the existing #films
in the DOM. But you could also create a dummy element, append the items to it, then append its contents to a container element in the DOM. The operation doesn't have to be a total replace. But the idea is that you only ever touch the DOM once.
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
See Template Literals for multi-line strings and string interpolation.
$endgroup$
$begingroup$
Thank you for your answer ! What do you mean by create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. How can do it can you please provide me more details about it.
$endgroup$
– Ced
12 hours ago
$begingroup$
@Ced sure. Will update my answer in a bit.
$endgroup$
– Joseph
12 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Ced is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212400%2fcreate-a-separate-json-syntax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I would recommend removing count
if all it does is represent the number of items in movies
. Otherwise, you'll have 2 sources of truth in the data (count
and the length
of movies
) and code that takes sides down the line (one that believes in count
and one that believes in the length of movies
). So as much as possible, avoid duplicating data.
Now it's not always feasible to recompute data all the time (i.e. if you had to filter thousands of rows to count "selected" items). This is one case where I'd put a fixed number instead of recomputing it from some property. Just make sure it's documented somewhere.
for (var i in movieData.movies) {
Use regular for
loops when looping through arrays. The problem with for-in
is that it runs through all enumerable properties, indices and other enumerable properties. You might be getting more than what's just in the array. An even better suggestion is to use array.forEach
.
$('#films').append(movieDiv);
The problem with this approach is that you're doing a jquery.append
on every item. That means the browser has to re-render every time you add an item. What I suggest is to create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. This way, the browser only ever renders once.
// Creates a that's in memory, not yet appended to the DOM.
const replacementFilms = $('', { id: '#films' })
movieData.movies.forEach(movie => {
var movieDiv = '...the markup...'
// Appends to our that's "in memory".
replacementFilms.append(movieDiv)
})
// Replace the #films in the DOM with the one in memory in one go.
In this example, I replaced the existing #films
in the DOM. But you could also create a dummy element, append the items to it, then append its contents to a container element in the DOM. The operation doesn't have to be a total replace. But the idea is that you only ever touch the DOM once.
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
See Template Literals for multi-line strings and string interpolation.
$endgroup$
$begingroup$
Thank you for your answer ! What do you mean by create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. How can do it can you please provide me more details about it.
$endgroup$
– Ced
12 hours ago
$begingroup$
@Ced sure. Will update my answer in a bit.
$endgroup$
– Joseph
12 hours ago
add a comment |
$begingroup$
I would recommend removing count
if all it does is represent the number of items in movies
. Otherwise, you'll have 2 sources of truth in the data (count
and the length
of movies
) and code that takes sides down the line (one that believes in count
and one that believes in the length of movies
). So as much as possible, avoid duplicating data.
Now it's not always feasible to recompute data all the time (i.e. if you had to filter thousands of rows to count "selected" items). This is one case where I'd put a fixed number instead of recomputing it from some property. Just make sure it's documented somewhere.
for (var i in movieData.movies) {
Use regular for
loops when looping through arrays. The problem with for-in
is that it runs through all enumerable properties, indices and other enumerable properties. You might be getting more than what's just in the array. An even better suggestion is to use array.forEach
.
$('#films').append(movieDiv);
The problem with this approach is that you're doing a jquery.append
on every item. That means the browser has to re-render every time you add an item. What I suggest is to create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. This way, the browser only ever renders once.
// Creates a that's in memory, not yet appended to the DOM.
const replacementFilms = $('', { id: '#films' })
movieData.movies.forEach(movie => {
var movieDiv = '...the markup...'
// Appends to our that's "in memory".
replacementFilms.append(movieDiv)
})
// Replace the #films in the DOM with the one in memory in one go.
In this example, I replaced the existing #films
in the DOM. But you could also create a dummy element, append the items to it, then append its contents to a container element in the DOM. The operation doesn't have to be a total replace. But the idea is that you only ever touch the DOM once.
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
See Template Literals for multi-line strings and string interpolation.
$endgroup$
$begingroup$
Thank you for your answer ! What do you mean by create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. How can do it can you please provide me more details about it.
$endgroup$
– Ced
12 hours ago
$begingroup$
@Ced sure. Will update my answer in a bit.
$endgroup$
– Joseph
12 hours ago
add a comment |
$begingroup$
I would recommend removing count
if all it does is represent the number of items in movies
. Otherwise, you'll have 2 sources of truth in the data (count
and the length
of movies
) and code that takes sides down the line (one that believes in count
and one that believes in the length of movies
). So as much as possible, avoid duplicating data.
Now it's not always feasible to recompute data all the time (i.e. if you had to filter thousands of rows to count "selected" items). This is one case where I'd put a fixed number instead of recomputing it from some property. Just make sure it's documented somewhere.
for (var i in movieData.movies) {
Use regular for
loops when looping through arrays. The problem with for-in
is that it runs through all enumerable properties, indices and other enumerable properties. You might be getting more than what's just in the array. An even better suggestion is to use array.forEach
.
$('#films').append(movieDiv);
The problem with this approach is that you're doing a jquery.append
on every item. That means the browser has to re-render every time you add an item. What I suggest is to create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. This way, the browser only ever renders once.
// Creates a that's in memory, not yet appended to the DOM.
const replacementFilms = $('', { id: '#films' })
movieData.movies.forEach(movie => {
var movieDiv = '...the markup...'
// Appends to our that's "in memory".
replacementFilms.append(movieDiv)
})
// Replace the #films in the DOM with the one in memory in one go.
In this example, I replaced the existing #films
in the DOM. But you could also create a dummy element, append the items to it, then append its contents to a container element in the DOM. The operation doesn't have to be a total replace. But the idea is that you only ever touch the DOM once.
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
See Template Literals for multi-line strings and string interpolation.
$endgroup$
I would recommend removing count
if all it does is represent the number of items in movies
. Otherwise, you'll have 2 sources of truth in the data (count
and the length
of movies
) and code that takes sides down the line (one that believes in count
and one that believes in the length of movies
). So as much as possible, avoid duplicating data.
Now it's not always feasible to recompute data all the time (i.e. if you had to filter thousands of rows to count "selected" items). This is one case where I'd put a fixed number instead of recomputing it from some property. Just make sure it's documented somewhere.
for (var i in movieData.movies) {
Use regular for
loops when looping through arrays. The problem with for-in
is that it runs through all enumerable properties, indices and other enumerable properties. You might be getting more than what's just in the array. An even better suggestion is to use array.forEach
.
$('#films').append(movieDiv);
The problem with this approach is that you're doing a jquery.append
on every item. That means the browser has to re-render every time you add an item. What I suggest is to create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. This way, the browser only ever renders once.
// Creates a that's in memory, not yet appended to the DOM.
const replacementFilms = $('', { id: '#films' })
movieData.movies.forEach(movie => {
var movieDiv = '...the markup...'
// Appends to our that's "in memory".
replacementFilms.append(movieDiv)
})
// Replace the #films in the DOM with the one in memory in one go.
In this example, I replaced the existing #films
in the DOM. But you could also create a dummy element, append the items to it, then append its contents to a container element in the DOM. The operation doesn't have to be a total replace. But the idea is that you only ever touch the DOM once.
'<li class="movie-item" data-id="' + movie.id + '">' +
'<a href="#">' +
'<img src="' + movie.thumb + '" width="280" height="150" />' +
'<span class="text-content"><i class="fa fa-chevron-up" aria-hidden="true"></i><br><br><i class="fa fa-4x fa-play-circle-o"></i><br><br>' + movie.title + '</span></span>' +
'</a>' +
'</li>';
See Template Literals for multi-line strings and string interpolation.
edited 12 hours ago
answered yesterday
JosephJoseph
22.6k21835
22.6k21835
$begingroup$
Thank you for your answer ! What do you mean by create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. How can do it can you please provide me more details about it.
$endgroup$
– Ced
12 hours ago
$begingroup$
@Ced sure. Will update my answer in a bit.
$endgroup$
– Joseph
12 hours ago
add a comment |
$begingroup$
Thank you for your answer ! What do you mean by create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. How can do it can you please provide me more details about it.
$endgroup$
– Ced
12 hours ago
$begingroup$
@Ced sure. Will update my answer in a bit.
$endgroup$
– Joseph
12 hours ago
$begingroup$
Thank you for your answer ! What do you mean by create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. How can do it can you please provide me more details about it.
$endgroup$
– Ced
12 hours ago
$begingroup$
Thank you for your answer ! What do you mean by create a DOM element in memory and append to that in your loop. Then, at the very end, slap the contents of that DOM element onto the page once. How can do it can you please provide me more details about it.
$endgroup$
– Ced
12 hours ago
$begingroup$
@Ced sure. Will update my answer in a bit.
$endgroup$
– Joseph
12 hours ago
$begingroup$
@Ced sure. Will update my answer in a bit.
$endgroup$
– Joseph
12 hours ago
add a comment |
Ced is a new contributor. Be nice, and check out our Code of Conduct.
Ced is a new contributor. Be nice, and check out our Code of Conduct.
Ced is a new contributor. Be nice, and check out our Code of Conduct.
Ced is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212400%2fcreate-a-separate-json-syntax%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown