MajorLeagueBaseball / g5-component

Event based Browserify component scaffold. :baseball:
http://majorleaguebaseball.github.io/g5-component/
Other
15 stars 8 forks source link

g5-component.js v3 #18

Open gbabula opened 7 years ago

gbabula commented 7 years ago

g5-component.js v3

Updates and improvements for the next major release.

--

Core

Build

Documentation

Suggestions

Other

kuhe commented 7 years ago

@melissalabbe Was your suggestion to clone or to share options?

melissalabbe commented 7 years ago

I think shared options would solve the particular issue that we've come across. To summarize, we have found that there is often a need to validate and/or extend the options that are passed into the component. Here's a specific example in standings component to illustrate:

Options are validated and extended in the custom model: https://github.mlbam.net/fed-packages-components/fed-component-baseball-standings/blob/master/src/scripts/model/master.js#L32

Even though the custom viewModel has a reference to the options that were passed in to the component, it won't have the extended options from the model so we pass those back in: https://github.mlbam.net/fed-packages-components/fed-component-baseball-standings/blob/master/src/scripts/viewModel/master.js#L62

Since this extending/validating options is a common need it seems like there should be support for this in the component master. We shouldn't need a custom model to perform this step, and once the options are extended we'd ideally be able to access them in the model and viewModel.

kuhe commented 7 years ago

With the new DI model I'm proposing, you can modify the opts in the constructor like so:

class YourComponent extends G5Component {
    constructor(opts) {
        // Here do anything you need to opts, and build the DI container.
        super(opts, implementation);
    }
}

// Traditional factory function top level for your component.
export default function (opts) {
    return new YourComponent(opts);
}

The super(opts, implementation) call will still copy the opts unless we specifically remove that, but you make your modifications before that, they will at least propagate to all MVM/Event elements.