ember.js - Editing a record in Ember without updating the UI -
i'm using ember , ember-data , i'm trying edit record. problem since inputs two-way-bound model, modifying in form changes model throughout application (even though don't commit changes). hence, changes reflect everywhere in site though aren't stored.
what ember way of editing records without modifying them until user confirms changes? there way clone models?
that's funny, myself ran this, came solution, , documented in own stack overflow question @ calling mymodel.save() returning outdated model. scroll down solution part, second bullet.
my perception pattern, while common, still not well-documented in ember community. example, see these recent discussions:
- https://stackoverflow.com/a/25350828/2308858 (see comments)
- http://discuss.emberjs.com/t/before-leaving-route/2328
i solved doing following:
- hook
willtransition
event in route that, whenever page doing editing transitioned out of, can execute code right before happens. note covers both clicking way out, or back-button'ing out. if user closes browser, these changes lost anyway, nothing needed handle situation. - now in hook, model
this.controller.get('model')
, @ value ofisdirty
this.controller.get('model').get('isdirty')
. see http://emberjs.com/guides/models/working-with-records/ discussion on how understandisdirty
. - if
isdirty
true, callrollback()
on model documented @ http://emberjs.com/api/data/classes/ds.model.html#method_rollback. - this revert unsaved changes rest of app looks consistent.
i have model orguser
has belongsto
relationship model orgperson
. relationship set { async: true }
. rolling orgperson
relationship worked me:
ember.route.extend({ actions: { willtransition: function( transition ) { this.controller.get('model.orgperson').then( function( value ) { if ( value.get('isdirty') ) { value.rollback(); } }); }, } }
note (a) may not need wrap isdirty
call in promise did (e.g. if don't declare { async: true }
, , (b) may access value.get('model')
if don't have relationship deal with.
note that, in experience, mymodel.rollback()
not rollback of relationships! may because i'm using { async: true }
every relationship, watch out for.
special @kingpin2k pointing me in right direction on original post.
Comments
Post a Comment