Giter Club home page Giter Club logo

pokemon_battle's Introduction

Pokemon Battle

Objectives
Build a simple pokemon battle game in Angular
Practice Control logic and Event Binding
Consume an external api

###Instructions:

  • Make sure to bower install.
  • For the moment, our pokemon data lives in mock_data.js.
  • Can you render all the pokemon names?
  • Can you attach an event listener to each name?
  • Can you show and hide content based on application state?

Your goal is to build a simple pokemon battle game!

When you're done it should look like this:

basic layout

You should be able to choose two pokemon: pick your fighters

You should see who won the match: winner

##Built in Directives

  • Repeating
  • Hiding and Showing
  • Event Binding

##Avoiding "The Flicker"

  • What's the difference between: {{ logic in here }} and <some_tag some_attribute="logic in here">?
  • What happens when you load the page and you use this: <img src="/path_to_file_{{ name }}">
  • What if name doesn't exist yet? (maybe we're waiting on an api response). What will your browser do?

Promises

Promises are a convenient way of dealing with api callbacks (and other asychronous processes).

Remember this pattern in jQuery?

    $.get("SOME_ENDPOINT", function(res){
        // do this when the api responds successfully
    })

And this:

$.get("SOME_ENDPOINT")
 .success(function(res){
    // do this when the api responds successfully
})
 .fail(function(res){
    // do this when the api fails
})

Promises give us new way of dealing with this problem.

  1. To do promises angular-style, we first need to inject the $q service into our controller or factory.

  2. We can then do the following:

function gimmie5() {
  var deffered = $q.defer();

  setTimeout(function(){
    deffered.resolve("FIVE!")   // 5 seconds later...
  }, 5000)

  return deffered.promise;      // returns immediately!
}

gimmie5().then(
  function(response){
      $scope.answer = response;
  }
);

But what about api requests? Those aren't always successful. How do we deal with errors? First, let's inject the $http service and then deal with both success and error.

function checkWeAreDoomed() {
  var deferred = $q.defer();

  $http
   .get("http://alloworigin.com/get?url=http://www.hasthelargehadroncolliderdestroyedtheworldyet.com") // HACK
   .success(function(response){
      deferred.resolve("NOPE");
    })
   .error(function (rejection) {
      deferred.reject("Ahhhhhh!");
   });

  return deferred.promise;
};

checkWeAreDoomed().then(
  function(response){
      $scope.answer = response; // "NOPE"
  },
  function(rejection){
      $scope.answer = rejection ; // "Ahhhhhh!"
  }
);

This pattern becomes waai more important when we're dealing with multiple controllers / models / services.

Reading:

Bonus reading:

pokemon_battle's People

Contributors

nathanallen avatar

Watchers

James Cloos avatar David Deuber avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.