Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

This

...

is

...

a

...

simple

...

number

...

guessing

...

game

...

written

...

in

...

javascript

...

using

...

Canjs

...

Index.html

Code Block

 <\!-\- This is the main html file for the guess a number game

&nbsp; &nbsp; &nbsp;It     It sets up a div as an attachment point for the view

&nbsp; &nbsp; &nbsp;Includes     Includes some helper CSS, and loads the necessary

&nbsp; &nbsp; &nbsp;javascript     javascript files

\-->

<\!doctype html>

<html>

<head>

&nbsp; &nbsp;    <title>Guess a number</title>

&nbsp; &nbsp;    <meta charset="utf-8">

&nbsp; &nbsp;    <link rel="stylesheet" href="css/bootstrap.min.css">

&nbsp; &nbsp;    <link rel="stylesheet" href="css/contacts.css">

</head>

<body>

<div id="guessView" ></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>

<script src="js/can.jquery.js"></script>

<script src="js/can.fixture.js"></script>

<script src="js/guess.js"></script>

</body></html>
----
h2. 

...

guessView.ejs

Code Block

<\!-\- This file is a very simple view that consists of a label and a text field

&nbsp; &nbsp; &nbsp;The     The label comes dynamically from the message field of the observable myData

&nbsp;  \-->

<form name='myform' >

<div >

<\!-\- This next line inserts the value of the message field on the myData observable.

&nbsp; &nbsp; &nbsp;Using     Using attr &nbsp;to to ftech it makes it "magic" in that the HTML wll update automatically

&nbsp; &nbsp; &nbsp;when     when the obervable is aware of a change to the value of the message field. \-->

<%= myData.attr('message'); %>

<input id='guess' type="text" name="guess" >

</div>

</form >

----
h2. 

...

guess.js

Code Block

// This is the application code for "guess a number'

// This sets up a callback triggered at the end of document load
// All can setup needs to be done after the DOM is ready
$(document).ready(function(){
&nbsp; &nbsp;   // This creates a new instance of the Guess controller
&nbsp; &nbsp;   // Its view will be affixed to the div named guessView in
&nbsp; &nbsp;   // the index.html and the observable GuessData will be passed
&nbsp; &nbsp;   // in for its use as the 'data' field on this.options
&nbsp; &nbsp;   new Guess('#guessView',{data:GuessData});
});


// This defines an obervable data block that has two fields
// The first is a message that will be displayed in the view
// The second is a random number between 0 and 99 inclusive
// that is hidden from the user
// The point of the game is to guess this random number
var GuessData = new can.Observe({
&nbsp; &nbsp;    message: "Enter a guess: ",
&nbsp; &nbsp;    guess: Math.floor(Math.random()*100)
});

// This creates a constructor called Guess that can create
// a guess controller when new'd.  &nbsp;It is used in the document loaded
// callback above to instance an actual controller
Guess = can.Control({
&nbsp; &nbsp;   // The init function is called when an instance is new'd
&nbsp; &nbsp;   init: function(){
&nbsp; &nbsp; &nbsp; &nbsp;       // can.vew compiles the view ejs
&nbsp; &nbsp; &nbsp; &nbsp;       // this.element.html attaches the compiled view at the dom point
&nbsp; &nbsp; &nbsp; &nbsp;       // specified in the constructor call
&nbsp; &nbsp; &nbsp; &nbsp;       this.element.html(can.view('views/guessView.ejs',{
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;           myData:this.options.data
&nbsp; &nbsp; &nbsp; &nbsp;       }));
&nbsp; &nbsp;   },
&nbsp; &nbsp;   // This method is automagically attached to the controller's associated
&nbsp; &nbsp;   // div and calls back to this function on form submit
&nbsp; &nbsp;   // It returns false to prevent actual submission to the web server
&nbsp; &nbsp;    "submit": function(event){
&nbsp; &nbsp; &nbsp; &nbsp;        DoGuess(parseFloat(document.myform.guess.value),this.options.data);
&nbsp; &nbsp; &nbsp; &nbsp;        document.myform.guess.value="";
&nbsp; &nbsp; &nbsp; &nbsp;        return false; // short circuit submit
&nbsp; &nbsp;    }
});

// This function checks the guess and updates the message field
// IMPORTANT: such updates must be done through obervable.attr(<key>,<new value>)
// or they wil not be observed
function DoGuess(guess,myData){
&nbsp; &nbsp;    if (guess<myData.guess){
&nbsp; &nbsp; &nbsp; &nbsp; myData.attr('message',"Too low, Guess again: ");
&nbsp; &nbsp; } else if (guess>myData.guess) {
&nbsp; &nbsp; &nbsp; &nbsp; myData.attr('message', "Too Highlow, Guess again: ");
&nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; myData.attr("message","You got it\!");
&nbsp; &nbsp; }
}

// This is the application code for "guess a number'

// This sets up a callback triggered at the end of document load

// All can setup needs to be done after the DOM is ready

$(document).ready(function()\{

&nbsp; &nbsp;// This creates a new instance of the Guess controller

&nbsp; &nbsp;// Its view will be affixed to the div named guessView in

&nbsp; &nbsp;// the index.html and the observable GuessData will be passed

&nbsp; &nbsp;// in for its use as the 'data' field on this.options

&nbsp; &nbsp;new Guess('#guessView',\{data:GuessData\}

);

\});

// This defines an obervable data block that has two fields

// The first is a message that will be displayed in the view

// The second is a random number between 0 and 99 inclusive

// that is hidden from the user

// The point of the game is to guess this random number

var GuessData = new can.Observe(\{

&nbsp; &nbsp; message: "Enter a guess: ",

&nbsp; &nbsp; guess: Math.floor(Math.random()*100)

\});

// This creates a constructor called Guess that can create

// a guess controller when new'd. &nbsp;It is used in the document loaded

// callback above to instance an actual controller

Guess = can.Control(\{

&nbsp; &nbsp;// The init function is called when an instance is new'd

&nbsp; &nbsp;init: function()\{

&nbsp; &nbsp; &nbsp; &nbsp;// can.vew compiles the view ejs

&nbsp; &nbsp; &nbsp; &nbsp;// this.element.html attaches the compiled view at the dom point

&nbsp; &nbsp; &nbsp; &nbsp;// specified in the constructor call

&nbsp; &nbsp; &nbsp; &nbsp;this.element.html(can.view('views/guessView.ejs',\{

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;myData:this.options.data

&nbsp; &nbsp; &nbsp; &nbsp;\}));

&nbsp; \},

&nbsp; &nbsp;// This method is automagically attached to the controller's associated

&nbsp; &nbsp;// div and calls back to this function on form submit

&nbsp; &nbsp;// It returns false to prevent actual submission to the web server

&nbsp; &nbsp; "submit": function(event)\{

&nbsp; &nbsp; &nbsp; &nbsp; DoGuess(parseFloat(document.myform.guess.value),this.options.data);

&nbsp; &nbsp; &nbsp; &nbsp; document.myform.guess.value="";

&nbsp; &nbsp; &nbsp; &nbsp; return false; // short circuit submit

&nbsp; &nbsp; \}

\});

// This function checks the guess and updates the message field

// IMPORTANT: such updates must be done through obervable.attr(<key>,<new value>)

// or they wil not be observed

function DoGuess(guess,myData)\{

&nbsp; &nbsp; if (guess<myData.guess){

myData.else if (guess>myData.guess) {
        myData.attr('message', "Too lowHigh, Guess again: ");

&nbsp; &nbsp; \}&nbsp;else if (guess>myData.guess) \{

myData.attr('message', "Too High, Guess again: ")

&nbsp; &nbsp; \}&nbsp;else \{

    } else {
        myData.attr("message","You got it\!"); &nbsp;

&nbsp; &nbsp; \}

\}
    }
}