1

Is it correct to do Ajax request from the "model" in Backbone.js? Is it more correct from the "router"? I don't use RESTful for Backbone (by other reasons). When I say "do Ajax request", I mean that Ajax functions are implemented in the model. That function can be called (and be) outside the model.

My web application doesn't use Routers (only the Views and Models). I don't want to have url routes in the browser when I nav in the application. What problems can there be?

AndraD
  • 2,830
  • 6
  • 38
  • 48
vicenrele
  • 971
  • 3
  • 19
  • 37

2 Answers2

5

Your router should be as clean as possible, so definitely not there.

In your model, you define the URL for your model.

In your view, you simply call model.fetch().

The most common pattern I use (and see) is to do this:

var MyApp.myModel = Backbone.Model.extend({
    url: '/someResourceUrl'
});

var MyApp.myView = Backbone.View.extend({
    initialize: function() {
        this.model.bind("change",this.render,this);
        this.model.fetch();
    },
    render: function() {
        alert('do awesome stuff here');
    }
});
Scott Silvi
  • 3,059
  • 5
  • 40
  • 63
  • Ok. But the Ajax request...where? – vicenrele Jan 18 '13 at 20:44
  • backbone automagically handles the ajax request when you call fetch. – Scott Silvi Jan 18 '13 at 21:07
  • Ok. Sorry. I've not commented that I don't use RESTful of Backbone (by other reasons). I've implemented Ajax functions in the model. – vicenrele Jan 18 '13 at 21:24
  • 2
    You should probably just override [Backbone.Sync](http://backbonejs.org/#Sync), which is the function that Backbone calls every time it attempts to read or save a model to the server. This means model.fetch() will simply use whatever custom ajax call you need. [Here is a SO post](http://stackoverflow.com/questions/5096549/how-to-override-backbone-sync) that describes how to do this. – Scott Silvi Jan 18 '13 at 23:12
  • Ok. Thanks for information. But, for the moment, I will have Ajax functions in the model. is this correct? – vicenrele Jan 19 '13 at 00:28
  • 1
    Sorry for delay - holiday for the weekend. Yes, you can override Backbone.sync at the global level, but you can also do it at the individual model level. So you override it where you need to, and can still call model.fetch(); – Scott Silvi Jan 21 '13 at 16:35
  • Where do you define `this.model`? – nnyby Dec 16 '14 at 19:03
1

It is correct to put the AJAX request in the model because it is business logic. This is according to general MVC principles as well as backbone.js documentation:

"If your application needs to gather data from the server, local storage, cookies, etc then the model is where this should happen. In fact, the model is the only place in the whole system that should know anything about XMLHttpRequest, for example. You may have code in the model layer that loads code for you and creates model objects." - http://michaux.ca/articles/mvc-architecture-for-javascript-applications

"Model–view–controller (MVC) is a software architecture pattern which separates the representation of information from the user's interaction with it.[1][2] The model consists of application data, business rules, logic, and functions." - http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

"Backbone.Model – Like a Rails model minus the class methods. Wraps a row of data in business logic." - http://backbonejs.org/

Caleb G
  • 171
  • 2
  • 3