/raphael/assets/app/model/GameModel.js

https://github.com/cormac/jasmine-tutorial · JavaScript · 82 lines · 38 code · 18 blank · 26 comment · 7 complexity · 9202e3d245b0bbe4554e6f0013111e1d MD5 · raw file

  1. /****************************************************************************
  2. * File Name: GameModel.js
  3. * Class Name: GameModel
  4. * Purpose: store data about the individual games: this data should be fed in
  5. * when the app starts
  6. * Creation Date: 05/09/11
  7. * Created By: Cormac McGuire
  8. *
  9. *
  10. * Updated By:
  11. * Update Date:
  12. * Description:
  13. ****************************************************************************/
  14. (function( Game, document, window, undefined ){
  15. //console.log( Backbone );
  16. myModel = Backbone.Model.extend({
  17. //Load initial data into our model here
  18. initialize: function(){
  19. },
  20. // Validate function - ensure all data is valid
  21. validate: function( attrs ){
  22. //console.log( attrs );
  23. //**** PREPARE THE VALIDATION ******************************
  24. var error = '';
  25. var sumScores = 0;
  26. var sumTimes = 0;
  27. //get the sum of the individual scores
  28. for ( score in attrs.levelScores ) {
  29. sumScores += attrs.levelScores[score];
  30. }
  31. //sum the individual times
  32. for ( time in attrs.levelTimes ) {
  33. sumTimes += attrs.levelTimes[time];
  34. }
  35. //**** DO THE VALIDATION ***********************************
  36. //verify that gamename is a string
  37. if( typeof( attrs.gamename) !== 'string' ){
  38. error += 'game name must be a string';
  39. }
  40. // validate that scores tally correctly
  41. if ( sumScores != attrs.gameScore ) {
  42. error += 'scores do not tally';
  43. };
  44. //validate that the times tally
  45. if ( sumTimes!= attrs.gameTime ) {
  46. error += 'times do not tally';
  47. };
  48. //fire the validation error if necessary
  49. if(error !== ''){
  50. //alert( error );//uncomment this line for debug
  51. console.log( error );
  52. return error;
  53. }
  54. }
  55. });
  56. timeModel = Backbone.Model.extend({
  57. });
  58. myModelCollection = Backbone.Collection.extend( {
  59. model : Game.GameModel,
  60. });
  61. Game.GameModelCollection = new myModelCollection();
  62. Game.GameModel = new myModel();
  63. Game.TimeModel = new timeModel();
  64. })( Ruabone.module( 'game' ) );