My Backbone JS application uses a bunch of web services and while some of the services are specific to certain views, there are some service calls that are used in many places in the site. Where should these be defined? Do they belong someplace like myApp.webServices
?
Imagine, for example, that the app uses a user directory web service and a weather data service, and that these are needed in many different places. Is this the way to do it?
myApp.webServices = {
userDirectory: {
getUser: function(userID) {
// Do service call
return foo;
}
},
weather: {
getWeather: function(cityName) {
// Do service call
return bar;
}
}
}
I would organize this just like I would organize it anywhere else: UserDirectory
and WeatherService
are not part of anything else, so they belong in their own modules.
I use ES6, so then wherever they're needed (in each of your views, for example):
import Weather from "services/Weather";
import Directory from "directories/Users";
No need to bind them to a monolith. The power of Backbone is that there's no requirement to do it any one way, so just make rational decisions about application structure, and then load the files according to that.