Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ember-data: How to make a Saving/Saved flash message

In order to make a little yellow "Saving"/"Saved" indicator message at the top of my app, I'd like to have a boolean property indicating if any ember-data records are currently in flight.

I tried this:

App.store = DS.Store.create
  isSaving: (->
    for record in this.get('recordCache')
      if record?.getPath('stateManager.currentState.name') == 'inFlight'
        return true
    return false
  ).property('[email protected]')

but then I discovered that recordCache is not observable.

I don't use transactions, only App.store.commit(), so I looked at App.store.get('defaultTransaction'), but it didn't yield anything useful.

I'm using the RESTAdapter, so if I can extend it into giving me this piece of information, that would work too.

like image 202
Jo Liss Avatar asked Jul 30 '12 17:07

Jo Liss


1 Answers

Why not extend model, controller and/or view:

DS.Model.reopen({
    didCreate: function() {
        this.set('persisted', true);
    },
    didUpdate: function() {
        this.set('persisted', true);       
    },

    resetPersistenceFlag: function(){
        this.set('persisted', false);
    }.observes('isDirty')

});

Ember.Controller.reopen({
    contentPersistedObserver: function() {
        if (this.get('content.persisted') === true) {
            //do stuff here
        }  
    }.observes('content.persisted')
});

Ember.View.reopen({
    controllerContentPersistedObserver: function() {
        if (this.get('controller.content.persisted') === true) {
            //do stuff here
        }  
    }.observes('controller.content.persisted')
});​

That way your controller/view will know when model is saved. It will fire for Create/Update controllers, but for controllers that manage arrays it will not.

like image 133
Kristaps Avatar answered Oct 22 '22 06:10

Kristaps