The proper way for communication with RESTful back-end

34 views
Skip to first unread message

Petar Aleksic

unread,
Apr 29, 2015, 11:08:35 AM4/29/15
to backbone-...@googlegroups.com
Hello,

I've just started building the front-end in Backbone Marionette, that is going to consume the RESTful API I've written. Naturally, since I am not experienced with Marionette, a whole bunch of questions came up. The first thing I was gonna do is build the models that are going to communicate with the back-end. Now, I am aware of Backbone.Model's already predefined functions such as fetch, save, destroy etc., but that just doesn't fit my needs, since I have much more comprehensive methods than just basic. So, e.g. I have an Activity model that could be liked, commented, and all kinds of operations. 
My approach was to make a something like ParentModel that every other model is going to extend. In ParentModel I have a function that looks like this:

request
: function(path, type, data, cb)
In that request function I send requests to back-end with $.ajax and consume response with callback. 

The Activity model would then, as said, extend the ParentModel and comprise a function: 
 like: function(){
         
this.request('/api/v1/activity/like/' + this.id, 'PUT', someData, function(response){
           
//do smth with response
       
})
 
}

My concerns at the time is that I currently do not benefit much from using Backbone.Model, and sync method thereof. 
I also use global $ for sending ajax request, which to me doesn't seem as feature-driven modular architecture to which I try to aspire to. 

I might add as well that I am almost sure that, generally, my approach is not quite suitable, so any advises and guidelines for my current issue and generally for developing in Backbone Marionette, are more than welcome!

Cheers and thank you!
 
Reply all
Reply to author
Forward
0 new messages