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

Shorthands for subview declaration #43

Closed
u840903 opened this issue Aug 15, 2014 · 11 comments
Closed

Shorthands for subview declaration #43

u840903 opened this issue Aug 15, 2014 · 11 comments

Comments

@u840903
Copy link

u840903 commented Aug 15, 2014

Wouldn't it be great if you could do like this when you declared subviews?

    subviews: {
        status: {
            container: '[role=status]',
            constructor: StatusView,
            // No model is set so it defaults to its parent model.
        }
    }
    subviews: {
        user: {
            container: '[role=user]',
            constructor: UserView,
            model: 'model.user' // Will set model to model.user and automatically wait for it.
        },
    }
    subviews: {
        todos: {
            container: '[role=todos]',
            constructor: TodoView,
            collection: 'model.todos' // Will set collection to model.todos and automatically wait for it.
        }
    }

Right now it looks like this in my code.

    subviews: {
       status: {
            container: '[role=status]',
            waitFor: 'model',
            prepareView: function (el) {
                return new StatusView({
                    el: el,
                    model: this.model
                });
            }
        },
        user: {
            container: '[role=user]',
            waitFor: 'model.user',
            prepareView: function (el) {
                return new UserView({
                    el: el,
                    model: this.model.user
                });
            }
        },
        todos: {
            container: '[role=todos]',
            constructor: TodoView,
            waitFor: 'model.todos',
            prepareView: function (el) {
                return new TodoView({
                    el: el,
                    collection: this.model.todos
                });
            }
        }
    }

I can implement it, but first I would like to know if I missing something and if it is a good idea. What are the downsides to auto assigning parent model as model? What are the downsides to the two new shorthands?

@wraithgar
Copy link
Contributor

So you're thinking about something that would automatically parse the subviews attribute and call registerSubview for everything there? When would that get called?

@u840903
Copy link
Author

u840903 commented Aug 15, 2014

Yep. Just adding more attributes to the prepareView function that is created in _parseSubview and the model or collection to waitfor. It would be called on subviewParent.on('change'). Just like it is now.

Something like this. (a very crude solution)

// ## _parseSubview
    // helper for parsing out the subview declaration and registering
    // the `waitFor` if need be.
    _parseSubview: function (subview, name) {
        var self = this;
        var opts = {
            selector: subview.container || '[role=' + subview.role + ']',
            waitFor: subview.waitFor || subview.model || subview.collection || '',
            prepareView: subview.prepareView || function (el) {
                return new subview.constructor({
                    el: el,
                    parent: self,
                    model: subview.model ? getPath(self, subview.model) : self.model,
                    collection: subview.collection ? getPath(self, subview.collection) : null
                });
            }
        };
        function action() {
            var el, subview;
            // if not rendered or we can't find our element, stop here.
            if (!this.el || !(el = this.get(opts.selector))) return;
            if (!opts.waitFor || getPath(this, opts.waitFor)) {
                subview = this[name] = opts.prepareView.call(this, el);
                subview.render();
                this.registerSubview(subview);
                this.off('change', action);
            }
        }
        // we listen for main `change` items
        this.on('change', action, this);
    }

@gr2m
Copy link

gr2m commented Sep 12, 2014

I'd love to see that added. What's status here?

@HenrikJoreteg
Copy link
Member

The default prepareView currently looks like this:

return new subview.constructor({
    el: el,
    parent: self
});

So the subview will have a reference to its parent so you within the subview you could reference it by doing this.parent.model for example. But it'd be nicer for the subview to just assume this.model of course.

I like where you're going with this. I'll play around with an implementation.

@u840903
Copy link
Author

u840903 commented Sep 14, 2014

I'm currently using this fork where shorthands are implemented and waitFor takes multiple dependencies.
#44

@bear
Copy link
Contributor

bear commented Oct 31, 2014

closing per commit

f707134

@bear bear closed this as completed Oct 31, 2014
@wraithgar
Copy link
Contributor

That's not in master yet

@wraithgar wraithgar reopened this Oct 31, 2014
@codepunkt
Copy link

Seems this isn't in master yet - please merge! 👍

@wraithgar
Copy link
Contributor

That commit is from this branch afaik #64 Which has merge conflicts still. @HenrikJoreteg what are the odds we could take another look at that PR this week?

@HenrikJoreteg
Copy link
Member

@wraithgar I'm a +1 on simplifying things here, but there's certainly some variance in opinions on how to actually do it, I think that's why things stalled. Philip raised some valid concerns as feedback to my original PR.

@cdaringe
Copy link
Member

cdaringe commented Sep 1, 2015

Henrik made some progress on this, but it wasn't quite all there yet. if someone wants to pick it up and run with it, feel free. however, given that it's not high priority, I am closing out until someone takes the reigns.

@cdaringe cdaringe closed this as completed Sep 1, 2015
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

7 participants