How can I add a base url for all backboneJS model / collection REST calls?

Lets have the following code:

( function($) { TeacherModel = Backbone.Model.extend({ defaults : { uid : -1, name : "" } }); TeachersCollection = Backbone.Collection.extend({ model : TeacherModel, url : function() { return "/path/to/api" + "/teacher"; } }); }()); 

I want to have "/path/to/api" as a global parameter for Backbone (I know that I can export it around the world to window , but I want to set it somewhere in Backbone)

Is there a way and for what is best?

+4
source share
1 answer

As I usually solve this, you need to create a base collection and inherit it, for example:

 BaseCollection = BackBone.Collection.extend({ path: '/path/to/api' }); TeachersCollection = BaseCollection.extend({ // path is now accessible from this and all other derived collections. }); 
+5
source

Source: https://habr.com/ru/post/1394644/


All Articles