-
Notifications
You must be signed in to change notification settings - Fork 32
Backbone Coding Standards
-
Base classes are used for Models, Collections, and Views to add common behavior.
-
Try to use small views and templates, as rendering (or re-rendering) is faster and the DOM is easier to maintain and manipulate.
-
No logic in the templates, as its too hard to test the logic and if there exist complex template structure try to use Mustache/Handlebars templates instead of Underscore micro-template feature.
-
No manual jQuery DOM manipulation, the DOM fragments should be rendered through the View objects.
-
Unit tests should be maintained using Qunit (Basic)/Jasmine(Advance) for site-wide scripts like models, views, collections, base classes and utility libraries.
-
The Backbone.Model.parse method should only be used for things like virtual fields. For example, the member model may create a name field that combines first and last name.
-
All dependencies must go through RequireJS require() or define() functions. No global state even for jQuery, Backbone, Underscore, etc.
-
jQuery allows you to defer execution of code until when the DOM is fully-loaded with
$(document).ready(...), or its short form,$(...). This is useful for getting everything set up once your HTML document is ready.A common anti-pattern is to put class definitions like views, models, collections etc inside these blocks. They are not necessary.
Try to Avoid this:$(function() { App.PhotoView = Backbone.View.extend({ ... }); });Your classes should be ready before the HTML DOM is. This will save you from running into problems later where certain classes may not be available at certain parts of your application.
Instead consider this:
App.PhotoView = Backbone.View.extend({ ... }); -
Naming convention in Object Oriented Design Pattern; Classes often start in uppercase letters, while instances start with lowercase letters. For names with multiple words use CamelCase and avoid using underscores.
Example of Classes:Home Authorization AboutUs ContactsExample of Instances:
home authorization aboutUs contacts -
Always keep your Backbone and Underscore core files updated; As there are few functions which won’t work for older core files and have visit to Backbone's Change Log.
-
If you are using any service URL;
One of the Way:
url: {
return 'http://maps.googleapis.com/maps/api/directions/json?origin=Pune';
}
"Instead you must develop some way like “change in once place and will effect globally”.
One of the better way:
url: {
return this.globalURL + “/maps/api/directions/” + this.get(‘resType’) + '?origin=' + this.get('city');
}
- In Backbone View; Use this.$el.find(...) instead of $(...). Because $ will search entire DOM while this.$el will search in that respected view.
- Part of a route can be made optional by surrounding it in parentheses (/:optional)
routes: {
"download": "download",
"download/:path": "download"
}
Instead do this
"download(/:path)": "download"