-
-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model Lifecycle Hooks #123
Conversation
import { hasMany } from "ember-data/relationships"; | ||
|
||
export default Model.extend({ | ||
comments: hasMany()o, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comments: hasMany(),
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. |
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 |
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... |
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 |
Model Lifecycle Hooks |
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. |
I absolutely agree. I am hoping that the discussion for this RFC clarifies which hooks are needed - maybe not all make sense.
💯% appreciated |
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. |
This would be great! ⛵ I've currently relied on hacks, but this looks much better. Two thoughts:
import Ember from 'ember';
import DS from 'ember-data';
let { attr, belongsTo, hasMany } = DS;
// CURRENT WORKAROUND
export default DS.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 0
Ember.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);
},
});
// FUTURE
export default DS.Model.extend({
children: hasMany('child', { async: true }),
childrenCount: 0, // consumed by computed properties outside this class
didReceiveRelationship: 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
|
Rendered.