Ember-data: how to make a saved / saved flash message

To make a small yellow “Save” / “Saved” message at the top of my application, I would like to have a boolean property indicating whether any data records of ember data are currently located.

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(' recordCache.@each.stateManager.currentState.name ') 

but then I found that recordCache not observed.

I do not use transactions, only App.store.commit() , so I looked at App.store.get('defaultTransaction') , but brought nothing useful.

I am using RESTAdapter, so if I can expand it by providing me this information, this will work too.

+6
source share
3 answers

Well, you could just create a base model with the didUpdate property, which handles the display of your notification, and then each of your models will expand it. This is really a stop gap solution, but it works so far:

 App.Model = DS.Model.extend didUpdate: -> # invoke your notification alert("All changes saved!") App.User = App.Model.extend() 

However ... this will fire for every record that is updated. Thus, you probably want to create your own notification class so that it does not open several saved notifications at once ... but this is up to you.

I am currently working on implementing some basic server verification methods from this pull request https://github.com/emberjs/data/pull/376 , and I hope that I will come up with a better way to monitor the status of commits (and, of course, I will come back here if I find it).

+2
source

Why not expand the 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') });​ 

This way your controller / view will know when the model will be saved. It will fire for Create / Update controllers, but for controllers that control arrays, it will not.

+4
source

There is a much simpler solution:

 App.MyView = Ember.View.extend({ init: function () { this._super(); var self = this; this.get('controller').content.on('didUpdate', function() { self.SomeCoolUpdateFunctionForMyView(self.$()); }); } }); 

self. $ () links to the jquery view of your view.

0
source

Source: https://habr.com/ru/post/921703/


All Articles