Intro
In this post I wanted to describe guideline for models' classes that are used in my project at work.As I have said in my previous post, Backbone gives you a freedom of choice, so you need to pay the price for this. You can find a lot of good tutorials in internet that will point you to a good way of writing backbone-based apps, nevertheless thare are some problems that are left behind and you still need to solve them somehow. I would recommend you to start with this seria of articles. It covers almost all basic steps to build backbone app from scratch and gives clear vision of Backbone fundamentals.
Model class role
Inside your web app you may find four basic roles of modules and classes:- Domain model
- Business logic
- Various infrastructure classes
- UI layer
Model class in Backbone app is responsible for domain entity representation and business logic related to I/O operations. It also covers life-cycle states of an entity. The same refers to collection classes but they are meant to be used with the items that belongs to the same model class.
Life cycle of model begins with the instantiation and fetching data. Attributes changes trigger change event that could be used for handling states changes.
Implementation
Let's develop very sample class for the article entity that may react on tagging.
Generic tips
- Initialize method is for declaration of handlers and data init. Do not put heavyweight logic there.
- Do not mix everyhting in one single method. Parse is for parsingm fetch is for fetching, save is for saving. Keep an eye on that during the code reviews ;)
- Url as attribute is good when url is static, but it's a very rare casem better to use url as method
States handling
So here is a sample model that is built according to the things I described above:- var ArticleModel = Backbone.Model.extend({
- initialize: function() {
- //Handler for tags changes
- this.on('change:tags',function(value){
- var id = this.id;
- //Iterate over changed tags
- _.each(value, function(item) {
- //Add id of article to them
- Tags.getByName(item).add(id);
- });
- });
- }
- });
- Usually, when newcomers start to write application they are trying to put all checks and triggers into model methods, cause it's more obvious to debug. But in this sad case code is getting messy day by day and one morning it will explode.
I/O
Another point is that server side may be designed for native apps that can use CURL library and work with all types of responses without any limitations. HTML5 is different, less powerful and more naughty.You have two options when developing I/O methods for your model and the server side is not REST+JSON:
- To override sync method and implement own Create/Read/Update/Delete calls to server. Good option when the server is still REST but gives different response format. Just override parse method and handle the response there.
- To implement own fetch/save/destroy methods. Good when server is not REST at all.
- var ArticleModel = Backbone.Model.extend({
- initialize: function() {
- //Handler for tags changes
- this.on('change:tags',function(value){
- var id = this.id;
- //Iterate over changed tags
- _.each(value, function(item) {
- //Add id of article to them
- Tags.getByName(item).add(id);
- });
- });
- },
- url: function() {
- return "http://api.com/articles/" + this.id + "?responseFormat=xml";
- },
- fetch: function(options) {
- $.ajax({
- url:this.url(),
- success:function(response) {
- this.set(this.parse(response));
- options.success && options.success.call(options.context || this,this);
- },
- context: this,
- method: "GET",
- dataType: "xml"
- });
- },
- parse: function(response) {
- return {
- journalist: $(response).find("> journalist").attr("name"),
- published: $(response).find("> published")
- }
- }
- });
- As you can see everything depend on your circumstances and requirements.
Outro
I outlined some aspects of models classes development that may save your nerves in your projects. I add more posts about views and routers classes guidelines soon.Thanks for your attention!