Uh oh!
There was an error while loading. Please reload this page.
Model Lifecycle Hooks - #123
Conversation
| import { hasMany } from "ember-data/relationships"; | ||
| export default Model.extend({ | ||
| comments: hasMany()o, |
stefanpenner
commented
Mar 6, 2016
Are these change hooks called synchronously, do they batch? For some of the hooks, this feels awfully like special purpose observers. Typically, we wan the world to settle, then be informed (maybe with hints) that stuff happened. So we can react and make changes against the settled state, rather then some intermediate state. |
tchak
commented
Mar 7, 2016
One thing that worries me is how you going to deal with model materialisation? Currently when you load but not accessing a bunch of models, only |
tchak
commented
Mar 7, 2016
You can always look on model prototype to check if materialisation is needed, but it would mean that adding a hook will have non trivial perf implications... |
pangratz
commented
Mar 7, 2016
I would say that the hooks are invoked after the data is set on the models and all relationships have been settled. Similar to the existing
@stefanpenner can you clarify which hooks you mean there?
This is an important thing to consider and I might haven't addressed this explicitly in the RFC. Thanks for pointing that out. I would say that the hooks are only invoked on |
stefanpenner
commented
Mar 7, 2016
Model Lifecycle Hooks |
stefanpenner
commented
Mar 7, 2016
Ya, I think that would be good. I think this is an important (and easily overlooked) aspect of all low level API design. It is worth noting, that the side-affects a user can introduce when a hook is invoked, must be taken into account when deciding if the hook should exist, and when it should be invoked. By exposing currently hard to observe or totally unobservable behavior, we are also locking down internal implementation to some degree. As such we shouldn't over-expose unless we must. Please note, I am merely trying to apply a counter balance here, hopefully this thought-path will help yield best-possible solution to this problem. Ember itself suffers from this, as several low-level API's overexpose our internals, and changing them (to fix bugs, or performance) is extremely non-trivial. |
pangratz
commented
Mar 7, 2016
I absolutely agree. I am hoping that the discussion for this RFC clarifies which hooks are needed - maybe not all make sense.
💯% appreciated |
fguillen
commented
Mar 15, 2016
I really would like to see an intuitive API for the "Ember Model Lifecycle". I have been struggling with this issue for a while:
I was looking for something as simple as:
And it has to be an observable property.
Triggered either when first load to the backend starts, when the reload starts or when the update starts.
Triggered either when first load to the backend ends, when the reload ends or when the update ends. |
sandstrom
commented
Jun 9, 2016
This would be great! ⛵ I've currently relied on hacks, but this looks much better. Two thoughts:
importEmberfrom'ember';importDSfrom'ember-data';let{ attr, belongsTo, hasMany }=DS;// CURRENT WORKAROUNDexportdefaultDS.Model.extend({children: hasMany('child',{async: true}),childrenCount: 0,// consumed by computed properties outside this class_childrenDidChange: function(){this.hasMany('children').ids().length;// will be 0Ember.run.next(this,'_deferred');}.observes('children.length'),_deferred(){this.hasMany('children').ids().length;// will be N (correct count)this.('childrenCount',this.hasMany('children').ids().length);},});// FUTUREexportdefaultDS.Model.extend({children: hasMany('child',{async: true}),childrenCount: 0,// consumed by computed properties outside this classdidReceiveRelationship: function(name,reference){if(reference==='children'){this.set('childrenCount',this.hasMany('children').ids().length);}},}); |
| How should this feature be introduced and taught to existing Ember users? | ||
| --> | ||
| This enhances the known programming model of hooks by adding one for additional |
There was a problem hiding this comment.
I don't think you mean "enhances"; perhaps you mean "leverages". In any case, this is unrelated to "How to Teach This". Note also that the mere fact that some programming model is known has nothing to do with whether it's the right approach for solving some particular problem.
With the introduction of In general, I'm fairly opposed to lifecycle hooks for objects that don't have a clear lifecycle. I think doing things at the source of the change with the full context of the change (the RecordData approach) is less likely to troll users or fall victim to order-of-operations/race conditions. Recommend for closing. |
runspired
commented
Apr 24, 2019
|
Rendered.