backbone.js - Is there a way to tell apart from user based changes to model and ones that came from the database? -
i trigger auto save on model every time changes, if change came ui (i.e. view). mean, if change came database, there no point in saving again, came database...
however fetch
, save
can trigger change
events (fetch
because brings potentially different state of model, , save
can bring id
newly created model)
so in change
event, i'd know caused change.
e.g. call view?:
this.model.set("foo", "bar"); //triggers change event foo's value changed
or result of sync operation?:
model.fetch(); //triggers change event model changed in db model.save(); //triggers change event id empty
is there way tell them apart?
once solution thought of wrapping view calls model's set
setattribute
method, , triggering custom changeduetouiaction
event, i'm sure has been solved before , i'm sure in better way...
or missing altogether , problem ask wrong question?
i'd there several custom solutions involving more or less boilerplate. , there easy one, though still adding bit of boilerplate.
(without major change backbone) can't know source of change (thinking it, how have access in model#set method?). have access in listener 2 objects. first 1 model changed. second options passed when calling set method. guess that:
// in view this.model.set('foo', 'bar', {'isfromui': true}); // in model this.listento(this, 'change', function(model, flags) { if(flags && flags.isfromui) this.save(); });
Comments
Post a Comment