|
| 1 | +var React = require('react'); |
| 2 | +var ReactStateMagicMixin = require('../../../../mixins/ReactStateMagicMixin'); |
| 3 | +var Alt = require('../../../../'); |
| 4 | +var AltManager = require('../../../../utils/AltManager'); |
| 5 | +var AppActions = require('../actions/AppActions'); |
| 6 | +var AppStore = require('../stores/AppStore'); |
| 7 | +var WeatherTab = require('./WeatherTab.jsx'); |
| 8 | + |
| 9 | +var manager = new AltManager(Alt); |
| 10 | +AppActions.setManager(manager); |
| 11 | + |
| 12 | +module.exports = React.createClass({ |
| 13 | + |
| 14 | + displayName: 'App', |
| 15 | + mixins: [ReactStateMagicMixin], |
| 16 | + statics: { registerStore: AppStore }, |
| 17 | + |
| 18 | + render: function() { |
| 19 | + var locationLinks = []; |
| 20 | + var weatherApp = null; |
| 21 | + |
| 22 | + if (this.state.location) { |
| 23 | + weatherApp = ( |
| 24 | + <WeatherTab |
| 25 | + alt={manager.getOrCreate(this.state.location)} |
| 26 | + location={this.state.location} /> |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + for (var location in manager.all()) { |
| 31 | + locationLinks.push( |
| 32 | + <li |
| 33 | + key={location} |
| 34 | + className={location === this.state.location ? 'selected' : null}> |
| 35 | + <a href="javascript:void(0);" onClick={this._onClickLocation.bind(this, location)}> |
| 36 | + {location} |
| 37 | + </a> |
| 38 | + </li> |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + return ( |
| 43 | + <div> |
| 44 | + <h1>AltManager - Weather Tabs App</h1> |
| 45 | + <div className="content"> |
| 46 | + <p> |
| 47 | + This is an example React/Flux/Alt app that shows the use of AltManager, a |
| 48 | + utility class for Alt.js. AltManager allows for multiple alt instances which is |
| 49 | + necessary to build apps that encapsulates a mini app inside of a large app. In |
| 50 | + this example we have a simple weather searcher. Each search you make will |
| 51 | + create a new tab which itself is a new Alt instance and has its own internal |
| 52 | + store & actions. Whatever you do in the alt instance is persisted even after |
| 53 | + you move to another tab. You can delete tabs which will delete that alt instance |
| 54 | + </p> |
| 55 | + <form className="search-box" onSubmit={this._onClickSubmit}> |
| 56 | + <input |
| 57 | + type="text" |
| 58 | + className="search-location" |
| 59 | + placeholder="Location eg. New York City, NY"/> |
| 60 | + <button>Search Weather</button> |
| 61 | + </form> |
| 62 | + <ul className="nav">{locationLinks}</ul> |
| 63 | + {weatherApp} |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + ); |
| 67 | + }, |
| 68 | + |
| 69 | + componentDidMount: function() { |
| 70 | + // we load San Francisco, CA on page load as an example |
| 71 | + AppActions.addLocation('San Francisco, CA'); |
| 72 | + }, |
| 73 | + |
| 74 | + _onClickSubmit: function(e) { |
| 75 | + e.preventDefault(); |
| 76 | + AppActions.addLocation(e.target.children[0].value.trim()); |
| 77 | + }, |
| 78 | + |
| 79 | + _onClickLocation: function(location) { |
| 80 | + AppActions.setLocation(location); |
| 81 | + } |
| 82 | +}); |
0 commit comments