Insomnia Listing

by Sam Fereday

HTML

<div ng-app="app">
  <div ng-controller="gamelisting">
    <ul ng-repeat="model in gamelist">
      <li>No games bro...</li>
    </ul>
    Add a new game:
    <input ng-model="addInput" placeholder="Add New" ng-submit="gameAdd()" />
    <button>Add</button>
  </div>
</div>

JavaScript

var app = angular.module('app', []);

function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}

// Game class
var Game = function(){
	this.id = "";
  this.title = "";
};

// User class
var User = function(){
	this.name = "";
	this.gameScored = [];
}

// Ctrl
app.controller('gamelisting', function($scope){

	$scope.gamelist = [];

	$scope.gameAdd = function(){
  		
      var newGame;
      var exists = $scope.gamelist.find(function(item){
      		return item.title.toLowerCase() === $scope.addInput.toLowerCase();
      });
      
      if(!exists) {
      		newGame = new Game();
          newGame.set({
          	id: guid(),
            title: $scope.addInput
          });
      }
      
  }	

});