Skip to content
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

derived children #113

Closed
flipside opened this issue Dec 3, 2014 · 4 comments
Closed

derived children #113

flipside opened this issue Dec 3, 2014 · 4 comments

Comments

@flipside
Copy link

flipside commented Dec 3, 2014

I've been looking at how cached derived properties work and figured out a relatively easy way to have them behave like children.

derived: {
    settings: {
        deps: ['id'],
        fn: function () {
            // get settings model from global settingsCollection
            var settings = settingsCollection.get(this.id);
            if (settings) {
                var previousSettings = this._cache['settings'];
                // only update listener if model has changed
                if (previousSettings !== settings) {
                    // if there's a previous model, stop listening to it
                    if (previousSettings)
                        this.stopListening(previousSettings);
                    // listen to new model and bubble its events
                    this.listenTo(settings, 'all', this._getEventBubblingHandler('settings'));
                }
            }
            return settings;
        }
    }
}

This seems pretty useful and might be worth adding this to state with a {listen: true} option added to derived properties to handle all this.

Let me know if I should make a pull request!

@aaronmccall
Copy link

This may be a good candidate for an extended version of ampersand-state or a mixin to it—something like ampersand-state-derivedChildren or some such.

@flipside
Copy link
Author

flipside commented Dec 5, 2014

don't think this can be mixed in, it touches too many core parts like createDerivedProperty which is used in extend.

got an initial stab at an implementation here

@flipside
Copy link
Author

flipside commented Dec 6, 2014

figured out an elegant solution, by allowing type:"state" on derived properties, we can use dataTypes.state.compare to handle events while leaving the door open for custom data types to have similar functionality. Also makes detecting changes in derived properties behave the same as regular properties.

derived-type implementation

@flipside
Copy link
Author

flipside commented Dec 9, 2014

submitted a pull request so shutting this down.

@flipside flipside closed this as completed Dec 9, 2014
@bear bear removed the question label Dec 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants