/**
 * models/drawModel.js
 * Module that defines winning numbers in the draw
 *
 * @return	{function} Model constructor extended from Bacbone.Model
 * @author  Ben on 18/Jul/2012
 */

define([
	'jquery',
	'backbone'
], function($) {

return Backbone.Model.extend({

	// Manually set prize and winning numbers
	setData: function(data) {
		var index = 0,
			numberList = {},
			prizeNameOfID = {};

		// map the prize id, prize name, and winning numbers
		$.each(data.prizes, function(key, value) {
			prizeNameOfID[index + 'thPrize'] = key;
			numberList[index + 'thPrize'] = value;
			index += 1;
		});

		this.clear({silent: true});
		this.set({
			months: data.months.replace(/0(?=[1-9])/g, ""),
			numberList: numberList,
			prizeNameOfID: prizeNameOfID
		});

		this.sortDataByType();
		this.createNumberNameMapping();
	},

	// Helper function: sort numbers into 2 categories
	sortDataByType: function() {
		var self = this,
			numberList = self.get('numberList'),
			prizeNameOfID = self.get('prizeNameOfID');

		this.set({
			// numbers that need every digit matching
			matchAll:
				$.map(numberList, function(numbers, id) {
					if ( prizeNameOfID[id] === "特別獎" || prizeNameOfID[id] === "特獎" )
						return numbers;
				}),
			// numbers that need at least three ending digits matching
			matchThree:
				$.map(numberList, function(numbers, id) {
					if ( prizeNameOfID[id] !== "特別獎" && prizeNameOfID[id] !== "特獎" )
						return numbers;
				})
		});
	},

	// Helper function: create prize number-name mapping
	createNumberNameMapping: function() {
		var numbers = $.map(this.get('numberList'), function(value, id) { return value; });
		var self = this,
			prizeNameOfID = this.get('prizeNameOfID'),
			prizeNameOfNumber = {};

		$.each(this.get('numberList'), function(id, numbers) {
			$.each(numbers, function(i, number) {
				prizeNameOfNumber[number] = prizeNameOfID[id];
			});
		});
		this.set('prizeNameOfNumber', prizeNameOfNumber);
	}
});

});
@@ -62,19 +62,17 @@
 bers in 
-the
+a
  draw%0A *
@@ -338,267 +338,34 @@
 var 
-index = 0,%0A%09%09%09numberList = %7B%7D,%0A%09%09%09prizeNameOfID = %7B%7D;%0A%0A%09%09// map the prize id, prize name, and winning numbers%0A%09%09$.each(data.prizes, function(key, value) %7B%0A%09%09%09prizeNameOfID%5Bindex + 'thPrize'%5D = key;%0A%09%09%09numberList%5Bindex + 'thPrize'%5D = value;%0A%09%09%09index += 1;%0A%09%09%7D)
+prizeNumbers = data.prizes
 ;%0A%0A%09
@@ -464,56 +464,28 @@
 %0A%09%09%09
-numberList: numberList,%0A%09%09%09prizeNameOfID
+prizeNumbers
 : prizeN
 ameO
@@ -480,23 +480,22 @@
 : prizeN
-ameOfID
+umbers
 %0A%09%09%7D);%0A%0A
@@ -666,68 +666,27 @@
 %0A%09%09%09
-numberList = self.get('numberList'),%0A%09%09%09prizeNameOfID = self
+prizeNumbers = this
 .get
@@ -693,23 +693,22 @@
 ('prizeN
-ameOfID
+umbers
 ');%0A%0A%09%09t
@@ -781,34 +781,36 @@
 :%0A%09%09%09%09$.map(
-n
+prizeN
 umber
-List
+s
 , function(n
@@ -809,34 +809,36 @@
 nction(numbers, 
-id
+name
 ) %7B%0A%09%09%09%09%09if ( pr
@@ -838,57 +838,30 @@
 if (
- prizeNameOfID%5Bid%5D === %22%E7%89%B9%E5%88%A5%E7%8D%8E%22 %7C%7C prizeNameOfID%5Bid%5D
+name === %22%E7%89%B9%E5%88%A5%E7%8D%8E%22 %7C%7C name
  ===
@@ -861,25 +861,24 @@
 ame === %22%E7%89%B9%E7%8D%8E%22
- 
 )%0A%09%09%09%09%09%09retu
@@ -984,26 +984,28 @@
 %09%09$.map(
-n
+prizeN
 umber
-List
+s
 , functi
@@ -1016,18 +1016,20 @@
 umbers, 
-id
+name
 ) %7B%0A%09%09%09%09
@@ -1037,57 +1037,30 @@
 if (
- prizeNameOfID%5Bid%5D !== %22%E7%89%B9%E5%88%A5%E7%8D%8E%22 && prizeNameOfID%5Bid%5D
+name !== %22%E7%89%B9%E5%88%A5%E7%8D%8E%22 && name
  !==
@@ -1064,17 +1064,16 @@
 !== %22%E7%89%B9%E7%8D%8E%22
- 
 )%0A%09%09%09%09%09%09
@@ -1209,151 +1209,19 @@
 var 
-numbers = $.map(this.get('numberList'), function(value, id) %7B return value; %7D);%0A%09%09var self = this,%0A%09%09%09prizeNameOfID = this.get('prizeNameOfID')
+self = this
 ,%0A%09%09
@@ -1269,18 +1269,20 @@
 et('
-n
+prizeN
 umber
-List
+s
 '), 
@@ -1290,18 +1290,20 @@
 unction(
-id
+name
 , number
@@ -1336,16 +1336,20 @@
 nction(i
+ndex
 , number
@@ -1388,25 +1388,12 @@
 %5D = 
-prizeNameOfID%5Bid%5D
+name
 ;%0A%09%09
@@ -1399,24 +1399,25 @@
 %09%09%7D);%0A%09%09%7D);%0A
+%0A
 %09%09this.set('
@@ -1463,12 +1463,13 @@
 %0A%09%7D%0A%7D);%0A%0A%7D);
+%0A

Replace numberList and prizeNameOfID with prizeNumbers