Last active
August 29, 2015 14:16
-
-
Save agmcleod/02ebc9c3d55babf0bf9f to your computer and use it in GitHub Desktop.
Dynamic list
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 lang="en"> | |
<head> | |
<title></title> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, maximum-scale=1.0" /> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> | |
</head> | |
<body> | |
<div ng-app="myapp"> | |
<div class="artists" ng-controller="ListArtists as artists"> | |
<a href="#" ng-click="artists.addArtistEvent()">Add artists</a> | |
<form ng-show="artists.showForm" ng-submit="artists.saveArtist()"> | |
<input type="text" name="name" ng-model="artists.artist_name" /> | |
<input type="submit" /> | |
</form> | |
<ul> | |
<li ng-repeat="artist in artists.artists track by $index">{{artist.name}}</li> | |
</ul> | |
</div> | |
</div> | |
<script type="text/javascript" src="angular.js"></script> | |
</body> | |
</html> |
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
var app = angular.module('myapp', []); | |
app.controller('ListArtists', function () { | |
this.artists = [{ | |
name: "Periphery" | |
}, { | |
name: "Dream Theater" | |
}]; | |
this.showForm = false; | |
this.artist_name = ""; | |
this.addArtistEvent = function () { | |
this.showForm = true; | |
} | |
this.saveArtist = function () { | |
this.artists.push({ | |
name: this.artist_name | |
}); | |
this.artist_name = ""; | |
this.showForm = false; | |
} | |
}); |
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 lang="en"> | |
<head> | |
<title></title> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, maximum-scale=1.0" /> | |
</head> | |
<body> | |
<div id="main"></div> | |
<script src="http://fb.me/react-0.12.2.js"></script> | |
<script src="components.js"></script> | |
</body> | |
</html> |
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
var App = React.createClass({ | |
appendArtist: function (e) { | |
e.preventDefault(); | |
var artists = this.state.artists; | |
artists.push({ | |
name: this.state.artistName | |
}); | |
this.setState({ | |
artists: artists, | |
artistsName: '' | |
}); | |
}, | |
componentDidMount: function () { | |
this.setState({ | |
artists: [{ | |
name: "Periphery" | |
}, { | |
name: "Dream Theater" | |
}] | |
}); | |
}, | |
getInitialState: function () { | |
return { | |
artistName: '', | |
artists: [], | |
showForm: false | |
} | |
}, | |
render: function () { | |
var artists = []; | |
for (var i = 0; i < this.state.artists.length; i++) { | |
var artist = this.state.artists[i]; | |
artists.push(<li>{artist.name}</li>)); | |
} | |
var form = null; | |
if (this.state.showForm) { | |
form = <form onSubmit={this.appendArtist}> | |
<input type="text" name="name" onKeyUp={this.setArtistName} /> | |
<input type="submit" /> | |
</form>; | |
} | |
return ( | |
<div> | |
<a href="#" onClick={this.showForm}>Add Artists</a> | |
{form} | |
<ul>{artists}</ul> | |
</div> | |
) | |
); | |
}, | |
setArtistName: function (e) { | |
this.setState({ | |
artistName: e.target.value | |
}); | |
}, | |
showForm: function () { | |
this.setState({showForm: true}); | |
} | |
}); | |
React.render( | |
App(), document.getElementById('main') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment