Last active
August 29, 2015 13:56
-
-
Save prabirshrestha/9128252 to your computer and use it in GitHub Desktop.
react countries search with backbone router
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
<!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) { | |
var search = qs ? qs.substr(2) : ''; | |
React.renderComponent(<App search={search}/>, document.getElementById('app')); | |
} | |
}); | |
var App = React.createClass({ | |
render: function () { | |
var countries; | |
if (this.props.search) { | |
countries = COUNTRIES.filter(function (c) { | |
return c.toLowerCase().indexOf(this.props.search.toLowerCase()) > -1; | |
}.bind(this)); | |
} else { | |
countries = COUNTRIES; | |
} | |
return <div> | |
<Search search={this.props.search}/> | |
<List countries={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