javascript - Backbone.js: smart update of a list of changed attributes -


i know problem have been discussed several times, not find answer 1 of many aspects, i'll try explain here.

model save() keeps sync between client , server. backbone.js pushes use in 1 of these 2 ways (afaik):

  1. save items of model: send server, attributes used on client side , attributes have not been changed. default new models (when model.isnew() returns true).
  2. save patch:true send changed attributes of model server. in backbone, means only attributes returned changedattributes() sent.

the point changedattributes() returns changes since last change event. useful models not updating time, , can afford automatically make ajax request on change.

but if have model changing (for example code/text editor or element can dragged/dropped , position should tracked), cannot save @ every single change event. need save @ time intervals, changed attributes since last time called save().

do think backbone.js provides support kind of synchronization? bakcbone.js track changes since last save() (and not since last change event)? or have "manyally"?

you'll have extend backbone models own base model.

var basemodel = backbone.model.extend({     save: function(key, val, options){         var attrs         if (key == null || typeof key === 'object') {           attrs = key;           options = val;         } else {            (attrs = {})[key] = val;         }           attrs = _.extend(this.unsaved || {}, attrs);         this.unsaved = {};         return backbone.model.prototype.save.call(this, attrs, options);     },     set: function(key, val, options) {        var attr        if (key == null) return this;        if (typeof key === 'object') {           attrs = key;           options = val;        } else {          (attrs = {})[key] = val;        }        this.unsaved = _.extend(this.unsaved || {}, attrs);        return backbone.model.prototype.save.call(this, attrs, options);     } }); 

totally untested code, should close need do.


Comments