ember.js - Prevent ember from updating templates when save() fails -


i'm building ember app on top of api , wonder, why ember updates template if saving fails because of invalid record (e.g. when api returns 422):

data.set('name', 'some_invalid_stuff');  data.save().then(function() {    // close modal   _this.closeform();    // disable editing mode   _this.set('isediting', false);  }, function (response) {    _this.set('errors', response.errors);   data.rollback();  }); 

when comment out data.rollback();, template gets updated invalid record, because api doesn't accept values. problem data.rollback(); strange flickering: first, record gets updated on template few milliseconds later, rollback fires , updates template old data.

especially if list sorted alphabetically, "double-updating" annoying.

is there way prevent template updating when save() fails?

in fact, templates gets updated instantly after data.set('name', 'some_invalid_stuff');.

i'm using latest stable ember release (1.5.1) , lastest canary version of ember-data (but tried latest beta version too).

the simple explanation ember updates template when model changes, , changed model. ember doesn't know or care whether have make change somewhere else before really takes affect, knows data give it.

if don't want behavior, you'll have duplicate data somehow. way can change model data , duplicated data won't change, thereby not updating template.

but recommend against that. of don't care template updates because of save operations should succeed. save operation failing should rare situation. me, 3 things make more pleasant user:

  1. do validation can on client side. don't let them send on data server reject, that's wasting round trip.

  2. if save fails because of network issue, try again. second time's charm.

  3. if save still doesn't succeed, don't rollback changes made. present error message , offer them opportunity try again later.

in other words: there's no built-in functionality handle these scenarios. have handle them in way makes sense application.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -