@@ -10,90 +10,91 @@ define([
10
10
"../views/CategoryView"
11
11
] , function ( $ , Backbone , CategoryModel , CategoriesCollection , CategoryView ) {
12
12
13
- // Extends Backbone.Router
14
- var CategoryRouter = Backbone . Router . extend ( {
13
+ // Extends Backbone.Router
14
+ var CategoryRouter = Backbone . Router . extend ( {
15
+ hasInitialized : false ,
15
16
16
- // The Router constructor
17
- initialize : function ( ) {
17
+ // The Router constructor
18
+ initialize : function ( ) {
18
19
19
- this . pagecontainer = $ ( ".ui-pagecontainer" ) . pagecontainer ( "instance" ) ;
20
+ // Instantiates a new Animal Category View
21
+ this . animalsView = new CategoryView ( { el : "#animals" , collection : new CategoriesCollection ( [ ] , { type : "animals" } ) } ) ;
20
22
21
- // Instantiates a new Animal Category View
22
- this . animalsView = new CategoryView ( { el : "#animals " , collection : new CategoriesCollection ( [ ] , { type : "animals " } ) } ) ;
23
+ // Instantiates a new Colors Category View
24
+ this . colorsView = new CategoryView ( { el : "#colors " , collection : new CategoriesCollection ( [ ] , { type : "colors " } ) } ) ;
23
25
24
- // Instantiates a new Colors Category View
25
- this . colorsView = new CategoryView ( { el : "#colors " , collection : new CategoriesCollection ( [ ] , { type : "colors " } ) } ) ;
26
+ // Instantiates a new Vehicles Category View
27
+ this . vehiclesView = new CategoryView ( { el : "#vehicles " , collection : new CategoriesCollection ( [ ] , { type : "vehicles " } ) } ) ;
26
28
27
- // Instantiates a new Vehicles Category View
28
- this . vehiclesView = new CategoryView ( { el : "#vehicles" , collection : new CategoriesCollection ( [ ] , { type : "vehicles" } ) } ) ;
29
+ // Tells Backbone to start watching for hashchange events
30
+ Backbone . history . start ( ) ;
29
31
30
- // Tells Backbone to start watching for hashchange events
31
- Backbone . history . start ( ) ;
32
+ } ,
32
33
33
- } ,
34
+ // Backbone.js Routes
35
+ routes : {
34
36
35
- // Backbone.js Routes
36
- routes : {
37
+ // When there is no hash bang on the url, the home method is called
38
+ "" : "home" ,
37
39
38
- // When there is no hash bang on the url, the home method is called
39
- " " : "home" ,
40
+ // When #category? is on the url, the category method is called
41
+ "category?:type " : "category"
40
42
41
- // When #category? is on the url, the category method is called
42
- "category?:type" : "category"
43
+ } ,
43
44
44
- } ,
45
+ // Home method
46
+ home : function ( ) {
45
47
46
- // Home method
47
- home : function ( ) {
48
-
49
- // Programatically changes to the categories page
50
- this . pagecontainer . change ( "#categories" , {
51
- reverse : false ,
52
- changeHash : false
53
- } ) ;
54
-
55
- } ,
56
-
57
- // Category method that passes in the type that is appended to the url hash
58
- category : function ( type ) {
48
+ // Programatically changes to the categories page
49
+ if ( this . hasInitialized ) {
50
+ $ ( "body" ) . pagecontainer ( "change" , "#categories" , {
51
+ reverse : false ,
52
+ changeUrl : false
53
+ } ) ;
54
+ }
55
+ } ,
59
56
60
- // Stores the current Category View inside of the currentView variable
61
- var currentView = this [ type + "View" ] ;
57
+ // Category method that passes in the type that is appended to the url hash
58
+ category : function ( type ) {
59
+ this . hasInitialized = true ;
60
+ // Stores the current Category View inside of the currentView variable
61
+ var currentView = this [ type + "View" ] ;
62
+ var that = this ;
62
63
63
- // If there are no collections in the current Category View
64
- if ( ! currentView . collection . length ) {
64
+ // If there are no collections in the current Category View
65
+ if ( ! currentView . collection . length ) {
65
66
66
- // Show's the jQuery Mobile loading icon
67
- $ . mobile . loading ( "show" ) ;
67
+ // Show's the jQuery Mobile loading icon
68
+ $ . mobile . loading ( "show" ) ;
68
69
69
- // Fetches the Collection of Category Models for the current Category View
70
- currentView . collection . fetch ( ) . done ( function ( ) {
70
+ // Fetches the Collection of Category Models for the current Category View
71
+ currentView . collection . fetch ( ) . done ( function ( ) {
71
72
72
- // Programatically changes to the current categories page
73
- this . pagecontainer . change ( "#" + type , {
73
+ // Programatically changes to the current categories page
74
+ $ ( "body" ) . pagecontainer ( "change" , "#" + type , {
74
75
reverse : false ,
75
- changeHash : false
76
+ changeUrl : false
76
77
} ) ;
77
- } ) ;
78
+ } ) ;
78
79
79
- }
80
+ }
80
81
81
- // If there already collections in the current Category View
82
- else {
82
+ // If there already collections in the current Category View
83
+ else {
83
84
84
- // Programatically changes to the current categories page
85
- this . pagecontainer . change ( "#" + type , {
85
+ // Programatically changes to the current categories page
86
+ $ ( "body" ) . pagecontainer ( "change" , "#" + type , {
86
87
reverse : false ,
87
- changeHash : false
88
+ changeUrl : false
88
89
} ) ;
89
90
90
- }
91
+ }
91
92
92
- }
93
+ }
93
94
94
- } ) ;
95
+ } ) ;
95
96
96
- // Returns the Router class
97
- return CategoryRouter ;
97
+ // Returns the Router class
98
+ return CategoryRouter ;
98
99
99
100
} ) ;
0 commit comments