|
1 |
| -_Coming Soon!_ |
| 1 | +# Integrating MDLv2 into frameworks |
2 | 2 |
|
3 |
| -> Simple frameworks (angular1, etc.) vs. Complex Frameworks (angular2, react, etc.) |
| 3 | +MDLv2 was designed to be integrated as easily as possible into any and all web frameworks. This |
| 4 | +document will walk you through strategies for integrating components into various types of |
| 5 | +frameworks. |
4 | 6 |
|
5 |
| -> Point to examples directory |
| 7 | +> NOTE: If you are interested in using MDLv2 components with custom elements, check out |
| 8 | +> our [Integrating into custom elements](./integrating-into-custom-elements.md) guide. |
| 9 | +
|
| 10 | +## Examples |
| 11 | + |
| 12 | +Our [examples](../examples) directory contains sample applications built in various frameworks, |
| 13 | +such as [react](../examples/react) and [angular2](../examples/angular2) that show how to wrap our |
| 14 | +components. If there's an example framework you'd like to see, or if you're a framework author who |
| 15 | +wants to provide an example, please consider letting us know and we'll work with you to have it |
| 16 | +contributed! |
| 17 | + |
| 18 | +## Approaches |
| 19 | + |
| 20 | +There are two approaches you can take for integrating our components into frameworks: the **simple** |
| 21 | +approach and the **advanced** approach. Both have their benefits and drawbacks, and are explained |
| 22 | +below. |
| 23 | + |
| 24 | +### The Simple Approach: Wrapping MDLv2 vanilla components. |
| 25 | + |
| 26 | +The easiest way to integrate MDLv2 into frameworks is to use our vanilla components directly. This |
| 27 | +works well for frameworks which assume they will be executed within the context of a browser, such |
| 28 | +as [angular v1](https://angularjs.org), [backbone.js](http://backbonejs.org/), or even things such as [jQuery plugins](https://learn.jquery.com/plugins/basic-plugin-creation/). |
| 29 | + |
| 30 | +The simple approach can be outlined as follows: |
| 31 | + |
| 32 | +1. Include the Component's CSS on the page any way you wish |
| 33 | +2. Create a **wrapper component** for your framework of choice, and add a property which will be |
| 34 | + set to the value of the MDLv2 Component. We'll call this `mdlComponent`. |
| 35 | +3. When the wrapper component is **initialized** (e.g. it is instantiated and attached to the DOM), |
| 36 | + _instantiate the MDLv2 component with a root element, and assign it to the `mdlComponent` |
| 37 | + property_. |
| 38 | +4. When the wrapper component is **destroyed** (e.g. it is unbound and detached from the DOM), call |
| 39 | + `mdlComponent.destroy()` to clean up the MDLv2 component. |
| 40 | + |
| 41 | +This general approach will work for almost all basic use-cases. For an example of this approach, |
| 42 | +check out [this plunk](https://plnkr.co/edit/b4v160c186ErrPG5vNza?p=preview) which |
| 43 | +shows how to wrap our textfield within an angular v1 component, as well as our button (with a |
| 44 | +ripple) within an attribute directive. |
| 45 | + |
| 46 | +Note that this approach will also work for [custom elements](https://developers.google.com/web/fundamentals/getting-started/primers/customelements). Use `connectedCallback` for initialization |
| 47 | +and `disconnectedCallback` for destruction. |
| 48 | + |
| 49 | +### The Advanced Approach: Using foundations and adapters |
| 50 | + |
| 51 | +Many modern front-end libraries/frameworks, such as react and angular2, wind up targeting more than |
| 52 | +just a web browser. For these frameworks - and for some highly advanced application architectures - |
| 53 | +a more robust approach is required. We provide foundations and adapters to accommodate this use |
| 54 | +case. |
| 55 | + |
| 56 | +> If you are interested in wrapping our components using foundations/adapters, you should first read |
| 57 | +> through our [architecture overview](./architecture.md) in order to familiarize yourself with the |
| 58 | +> general concepts behind them. |
| 59 | +
|
| 60 | +Every component comes with a complementary foundation class, which is usually called |
| 61 | +`MDLComponentFoundation`, where `MDLComponent` is the name of a component. For example, we have an |
| 62 | +[MDLSimpleMenuFoundation](../packages/mdl-menu/simple/foundation.js) that is used by our |
| 63 | +[MDLSimpleMenu](../packages/mdl-menu/simple/index.js) component, and which are both exported |
| 64 | +publicly. |
| 65 | + |
| 66 | +In order to implement a component via a foundation, take the following steps: |
| 67 | + |
| 68 | +1. Include the component's CSS on the page any way you wish |
| 69 | +2. Add an instance property to your component which will be set to the proper foundation class. |
| 70 | + We'll calls this `mdlFoundation`. |
| 71 | +3. Instantiate a foundation class, passing it a properly configured adapter as an argument |
| 72 | +4. When your component is initialized, call `mdlFoundation.init()` |
| 73 | +5. When your component is destroyed, call `mdlFoundation.destroy()` |
| 74 | + |
| 75 | +Because of the nature of our components, some of the adapter APIs can be quite complex. However, we |
| 76 | +are working as hard as we can to make writing adapters as easy and predictable as possible: |
| 77 | + |
| 78 | +- Adapters are strictly versioned: _any_ change to an adapter interface - associative or not - is |
| 79 | + considered breaking and will cause a major version update of the component. |
| 80 | +- Every adapter interface is thoroughly documented within each component's README |
| 81 | +- Most adapter methods are one-liners, and for those that aren't, we provide `util` objects which |
| 82 | + implement those methods. |
| 83 | +- We try and provide guidance on different ways to implement certain adapter methods that may seem |
| 84 | + ambiguous |
| 85 | +- We plan on creating Type Definitions for our adapters in the future so that TypeScript users can |
| 86 | + validate that their interface conforms correctly to the adapter's specification. |
| 87 | + |
| 88 | +Our [examples](../examples) directory contains sample code for various frameworks showing how |
| 89 | +adapters can be implemented. We encourage you to take a look through those applications to get a |
| 90 | +sense of how you might build your own. |
| 91 | + |
| 92 | +> Please file an issue with us if there are certain snags you've ran into trying to implement an |
| 93 | + adapter, or if you feel that we can provide better guidance on a particular problem. This is |
| 94 | + definitely something we want to know about. |
0 commit comments