1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "dojox/mvc", |
---|
4 | "dojox/mvc/StatefulModel", |
---|
5 | "dojo/data/ItemFileWriteStore" |
---|
6 | ], function(declare, mvc, StatefulModel, ItemFileWriteStore){ |
---|
7 | |
---|
8 | return declare("dojox.mvc.tests.models.LoanWizardModel", [StatefulModel], { |
---|
9 | |
---|
10 | // data store for pie chart |
---|
11 | chartStore: new ItemFileWriteStore({ data: { |
---|
12 | "hierarchical" : false, |
---|
13 | "identifier" : "id", |
---|
14 | "items" : [ |
---|
15 | { "id" : "mortgage", "x" : 1, "y" : 0 }, |
---|
16 | { "id" : "taxes", "x" : 2, "y" : 0 }, |
---|
17 | { "id" : "otherhousing", "x" : 3, "y" : 0 } |
---|
18 | ] |
---|
19 | }}), |
---|
20 | |
---|
21 | constructor: function (args) { |
---|
22 | // try to precompute address fields from the zipcode and country... |
---|
23 | mvc.bindInputs([this.Zip, this.Country], dojo.hitch(this, this._lookupAddrs)); |
---|
24 | // simple dependence of percentages on input values and total |
---|
25 | mvc.bindInputs([this.Mortgage, this.Taxes, this.OtherHousing, this.BaseIncome, this.BonusIncome], dojo.hitch(this, this._recomputeTotalAndPercentages)); |
---|
26 | |
---|
27 | mvc.bind(this.HousingPercent, "value", this.HousingPercent, "valid", dojo.hitch(this, this._isHousingLessThanOrEqualToThirtyThreePercent), true); |
---|
28 | |
---|
29 | mvc.bind(this.HousingPercent, "value", this.HousingPercent, "relevant", dojo.hitch(this, this._nonZeroRelevance), true); |
---|
30 | mvc.bind(this.TotalHousing, "value", this.TotalHousing, "relevant", dojo.hitch(this, this._nonZeroRelevance), true); |
---|
31 | |
---|
32 | this._recomputeTotalAndPercentages(); // get things going first time... |
---|
33 | }, |
---|
34 | |
---|
35 | _lookupItem: function( dataSource, identity ) { |
---|
36 | var found_item; |
---|
37 | dataSource.fetchItemByIdentity( { "identity": identity, |
---|
38 | "onItem": function (item) { found_item = item; } } ); |
---|
39 | return found_item; |
---|
40 | }, |
---|
41 | |
---|
42 | _lookupAddrs: function() { |
---|
43 | if ( this.Zip.get("value") == null || isNaN(this.Zip.get("value"))) return; |
---|
44 | var pThis = this; |
---|
45 | var query = { "postalcode": this.Zip.get("value"), "country": this.Country.get("value") }; |
---|
46 | var xhrArgs = { |
---|
47 | url: "zips/"+this.Zip.get("value")+".json", |
---|
48 | sync: true, |
---|
49 | content: query, |
---|
50 | preventCache: true, |
---|
51 | handleAs: "json", |
---|
52 | load: function(data, io) { |
---|
53 | pThis.City.set("value", data.postalcodes[0].placeName ); |
---|
54 | pThis.County.set("value", data.postalcodes[0].adminName2 ); |
---|
55 | pThis.State.set("value", data.postalcodes[0].adminCode1 ); |
---|
56 | pThis.Zip.set("valid", true ); |
---|
57 | }, |
---|
58 | error: function (data) { |
---|
59 | // we couldn't find this country/zip combination...clear the fields and set validity=false |
---|
60 | pThis.City.set("value", "" ); |
---|
61 | pThis.County.set("value", "" ); |
---|
62 | pThis.State.set("value", "" ); |
---|
63 | pThis.Zip.set("valid", false ); |
---|
64 | } |
---|
65 | }; |
---|
66 | //Call the synchronous xhrGet |
---|
67 | var deferred = dojo.xhrGet(xhrArgs); |
---|
68 | }, |
---|
69 | |
---|
70 | _recomputeTotalAndPercentages: function() { |
---|
71 | var mortgage = parseInt(this.Mortgage.get("value")); |
---|
72 | var taxes = parseInt(this.Taxes.get("value")); |
---|
73 | var otherHousing = parseInt(this.OtherHousing.get("value")); |
---|
74 | var totalHousing = mortgage + taxes + otherHousing; |
---|
75 | |
---|
76 | var baseIncome = parseInt(this.BaseIncome.get("value")); |
---|
77 | var bonusIncome = parseInt(this.BonusIncome.get("value")); |
---|
78 | var totalIncome = baseIncome + bonusIncome; |
---|
79 | |
---|
80 | var housingPercentage = Math.round(totalHousing / totalIncome * 100); |
---|
81 | |
---|
82 | this.HousingPercent.set("value", housingPercentage); |
---|
83 | this.TotalHousing.set("value", totalHousing); |
---|
84 | this.TotalIncome.set("value", totalIncome); |
---|
85 | |
---|
86 | // map the values into the data source structure required for chart display as well... |
---|
87 | var mortgageItem = this._lookupItem(this.chartStore, "mortgage"); |
---|
88 | var taxesItem = this._lookupItem(this.chartStore, "taxes"); |
---|
89 | var otherItem = this._lookupItem(this.chartStore, "otherhousing"); |
---|
90 | this.chartStore.setValue(mortgageItem, "y", mortgage); |
---|
91 | this.chartStore.setValue(taxesItem, "y", taxes); |
---|
92 | this.chartStore.setValue(otherItem, "y", otherHousing); |
---|
93 | }, |
---|
94 | |
---|
95 | _isHousingLessThanOrEqualToThirtyThreePercent: function(newValue) { |
---|
96 | return newValue <= 33; |
---|
97 | }, |
---|
98 | |
---|
99 | _nonZeroRelevance: function(newValue) { |
---|
100 | if ( newValue > 0 ) return true; |
---|
101 | else return false; |
---|
102 | } |
---|
103 | |
---|
104 | }); |
---|
105 | }); |
---|