Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to override fetch() and save() directly from the Model?

Tags:

backbone.js

I don't want to override Backbone.sync() as some of my models will actually use the standard sync().

Is it good practice to override fetch() and save() directly from the Model?

like image 960
julien_c Avatar asked Oct 07 '22 00:10

julien_c


1 Answers

It is not necessary to override sync globally. You can do it per Model/Collection ie,

var MyModel = Backbone.Model.extend({
    sync: customSync,
    ...
});

This avoids overriding Backbone.sync globally.

Now if you do not need to implement a full sync, for instance it you only need to override fetch, you can of course do so on a per model basis too.

Presumably you have seen Backbone.sync being overriden by the localStorage version. This is indeed not necessary (I would think it is bad practice). It is sufficient to just define the custom sync function and let models/collections use it.

like image 57
ggozad Avatar answered Oct 13 '22 11:10

ggozad