Created
February 21, 2014 04:11
-
-
Save prabirshrestha/9128593 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
</html> | |
<!DOCTYPE HTML> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div id="app"></div> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script> | |
<script src="http://backbonejs.org/backbone.js"></script> | |
<script src="http://fb.me/react-with-addons-0.9.0.js"></script> | |
<script src="http://fb.me/JSXTransformer-0.9.0.js"></script> | |
<script type="text/jsx"> | |
/** @jsx React.DOM */ | |
var COUNTRIES = ['United States', 'Canada', 'Afghanistan', 'Albania', 'Algeria']; | |
var Router = Backbone.Router.extend({ | |
routes: { | |
'' : 'index', | |
'?*qs' : 'index' | |
}, | |
index: function (qs) { | |
console.log('route index exectued'); | |
var search = qs ? qs.substr(2) : ''; | |
React.renderComponent(<App search={search}/>, document.getElementById('app')); | |
} | |
}); | |
var App = React.createClass({ | |
getInitialState: function () { | |
return { countries: [] }; | |
}, | |
componentWillMount: function () { | |
this.fetchCountries(); | |
}, | |
shouldComponentUpdate: function(nextProps, nextState) { | |
if (nextProps.search !== this.props.search) { | |
this.fetchCountries(); | |
} | |
return true; | |
}, | |
fetchCountries: function () { | |
// fake ajax call | |
setTimeout(function () { | |
var countries = COUNTRIES; | |
if (this.props.search) { | |
countries = COUNTRIES.filter(function (c) { | |
return c.toLowerCase().indexOf(this.props.search.toLowerCase()) > -1; | |
}.bind(this)); | |
} | |
console.log('fetched'); | |
this.setState({ countries: countries }); | |
}.bind(this), 2000); | |
}, | |
render: function () { | |
return <div> | |
<Search search={this.props.search}/> | |
<List countries={this.state.countries}/> | |
</div>; | |
} | |
}); | |
var List = React.createClass({ | |
render: function () { | |
return <div> | |
<ul> | |
{this.props.countries.map(function (country) { | |
return <Item key={country} country={country}/> | |
})} | |
</ul> | |
</div>; | |
} | |
}); | |
var Item = React.createClass({ | |
render: function () { | |
return <li> | |
{this.props.country} | |
</li>; | |
} | |
}); | |
var Search = React.createClass({ | |
handleSearch: function (e) { | |
e.preventDefault(); | |
router.navigate('?q=' + this.refs.search.getDOMNode().value, true); | |
}, | |
render: function () { | |
return <div> | |
<input ref="search" type="text" defaultValue={this.props.search} /> | |
<input type="submit" onClick={this.handleSearch}/> | |
</div>; | |
} | |
}); | |
window.router = new Router(); | |
Backbone.history.start(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment