Skip to content

Extract class controller declaration from component declaration #144

Open
@aaronte

Description

@aaronte

Hey, currently the x.component.js is declared as following where the controller is declared inline with the component declaration:

/* ----- todo/todo-form/todo-form.component.js ----- */
import templateUrl from './todo-form.html';

export const TodoFormComponent = {
    bindings: {
        todo: '<',
        onAddTodo: '&'
    },
    templateUrl,
    controller: class TodoFormComponent {
        constructor(EventEmitter) {
            'ngInject';
            this.EventEmitter = EventEmitter;
        }

        $onChanges(changes) {
            if (changes.todo) {
                this.todo = Object.assign({}, this.todo);
            }
        }

        onSubmit() {
            if (!this.todo.title) {
                return;
            }
            // with EventEmitter wrapper
            this.onAddTodo(
                this.EventEmitter({
                    todo: this.todo
                })
            );
            // without EventEmitter wrapper
            this.onAddTodo({
                $event: {
                    todo: this.todo
                }
            });
        }
    }
};

I was wondering if there's a reason why you chose to do it inline instead of declaring it first before using it like the following:

/* ----- todo/todo-form/todo-form.component.js ----- */
import templateUrl from './todo-form.html';

class TodoFormComponent {
    constructor(EventEmitter) {
        'ngInject';
        this.EventEmitter = EventEmitter;
    }

    $onChanges(changes) {
        if (changes.todo) {
            this.todo = Object.assign({}, this.todo);
        }
    }

    onSubmit() {
        if (!this.todo.title) {
            return;
        }
        // with EventEmitter wrapper
        this.onAddTodo(
            this.EventEmitter({
                todo: this.todo
            })
        );
        // without EventEmitter wrapper
        this.onAddTodo({
            $event: {
                todo: this.todo
            }
        });
    }
}

export const TodoFormComponent = {
    bindings: {
        todo: '<',
        onAddTodo: '&'
    },
    templateUrl,
    controller: TodoFormComponent
};

I think this makes it more readable and lessens indention? Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions