patternjavascriptMinor
Managing article information
Viewed 0 times
managingarticleinformation
Problem
I want you to give me some tips on what I could have done better and is considered "bad practice". The code works, but I think there are better ways to do stuff, so please let me know.
```
var Artikel = Backbone.Model.extend({
urlRoot: 'api/items.json',
defaults: {
titel: 'Titel niet opgegeven',
url_titel: 'unieke sleutel urltitel',
img_path: 'geen image toegevoegd',
commentaar: 'Commentaar niet opgegeven',
categorie: 'Categorie niet opgegeven',
waardering: 0,
artikel: 'Artikel niet opgegeven'
},
initialize: function() {
if (!this.get('description')) {
var lazy = 'This user was too lazy too add a description';
this.set('description', lazy);
}
}
});
//Mijn collection wordt voorzien van een sorteerfunctie om niet met 'asc' en 'desc' te moeten werken kan ik gebruik maken van 1 en -1
var Artikels = Backbone.Collection.extend({
model: Artikel,
sortAttribute: "categorie",
sortDirection: 1,
sortArtikels: function(attr) {
this.sortAttribute = attr;
this.sort();
},
comparator: function(a, b) {
var a = a.get(this.sortAttribute),
b = b.get(this.sortAttribute);
if (a == b) return 0;
if (this.sortDirection == 1) {
return a > b ? 1 : -1;
} else {
return a 0) {
var querystr = arguments[0].replace(/\+/g, "%20");
$("#searchQuery").val(decodeURIComponent(querystr));
}
},
events: {
'submit #searchForm': function(ev) {
$form = $(ev.currentTarget);
document.location = '#/search/' + encodeURIComponent($("#searchQuery", $form).val()).replace(/%20/g, '+');
return false;
}
}
});
//De resultsview laat de gevonden resultaten zien
var ArtikelSearchResultsView = Backbone.View.extend({
el: '#searchResults',
render: function(query_encoded) {
var query = decodeURICo
```
var Artikel = Backbone.Model.extend({
urlRoot: 'api/items.json',
defaults: {
titel: 'Titel niet opgegeven',
url_titel: 'unieke sleutel urltitel',
img_path: 'geen image toegevoegd',
commentaar: 'Commentaar niet opgegeven',
categorie: 'Categorie niet opgegeven',
waardering: 0,
artikel: 'Artikel niet opgegeven'
},
initialize: function() {
if (!this.get('description')) {
var lazy = 'This user was too lazy too add a description';
this.set('description', lazy);
}
}
});
//Mijn collection wordt voorzien van een sorteerfunctie om niet met 'asc' en 'desc' te moeten werken kan ik gebruik maken van 1 en -1
var Artikels = Backbone.Collection.extend({
model: Artikel,
sortAttribute: "categorie",
sortDirection: 1,
sortArtikels: function(attr) {
this.sortAttribute = attr;
this.sort();
},
comparator: function(a, b) {
var a = a.get(this.sortAttribute),
b = b.get(this.sortAttribute);
if (a == b) return 0;
if (this.sortDirection == 1) {
return a > b ? 1 : -1;
} else {
return a 0) {
var querystr = arguments[0].replace(/\+/g, "%20");
$("#searchQuery").val(decodeURIComponent(querystr));
}
},
events: {
'submit #searchForm': function(ev) {
$form = $(ev.currentTarget);
document.location = '#/search/' + encodeURIComponent($("#searchQuery", $form).val()).replace(/%20/g, '+');
return false;
}
}
});
//De resultsview laat de gevonden resultaten zien
var ArtikelSearchResultsView = Backbone.View.extend({
el: '#searchResults',
render: function(query_encoded) {
var query = decodeURICo
Solution
I don't see major refactorings in your code. However:
-
Use triple-equals (see here for more) when doing comparisons with zero or one (and probably the part with
-
Pre-compile templates rather than doing them on-the-fly for better performance
-
You are using Backbone, jQuery, and Underscore (or lodash) - re-examine whether you need all of these libraries. Less code means a faster loading page. (On the other hand, you might need the cross-browser compatibility provided by jQuery - your call.)
-
http://youmightnotneedjquery.com
-
http://vanilla-js.com/
-
Use triple-equals (see here for more) when doing comparisons with zero or one (and probably the part with
a == b, unless you know you want otherwise)-
Pre-compile templates rather than doing them on-the-fly for better performance
-
You are using Backbone, jQuery, and Underscore (or lodash) - re-examine whether you need all of these libraries. Less code means a faster loading page. (On the other hand, you might need the cross-browser compatibility provided by jQuery - your call.)
-
http://youmightnotneedjquery.com
-
http://vanilla-js.com/
Context
StackExchange Code Review Q#39843, answer score: 3
Revisions (0)
No revisions yet.