Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / app / nested-loading / app / controller / Books.js
1 /**
2  * Books controller
3  * Used to manage books; showing the first book, showing a spefied book, loading books, and showing their
4  * related views
5  */
6 Ext.define('Books.controller.Books', {
7     extend: 'Ext.app.Controller',
8     
9     models: ['Book'],
10     stores: ['Books', 'Reviews'],
11     
12     refs: [
13         {ref: 'bookSideBar', selector: 'booksidebar'},
14         {ref: 'bookView',    selector: 'bookview'},
15         {ref: 'reviewList',  selector: 'reviewlist'}
16     ],
17     
18     init: function() {
19         var me = this;
20         
21         me.control({
22             'booksidebar': {
23                 selectionchange: me.onSideBarSelectionChange
24             }
25         });
26         
27         me.getBooksStore().on({
28             scope: me,
29             load : me.onBooksStoreLoad
30         });
31     },
32     
33     onLaunch: function() {
34         this.getBookSideBar().bindStore(this.getBooksStore());
35     },
36     
37     onSideBarSelectionChange: function(view, records) {
38         if (records.length) {
39             this.showBook(records[0]);
40         }
41     },
42     
43     /**
44      * Called when the books store is loaded.
45      * Checks if there are any records, and if there are, it delegates to show the first record
46      * as well as selecting that record in the sidebar
47      */
48     onBooksStoreLoad: function(store, records) {
49         Ext.defer(function() {
50             if (records.length) {
51                 var record = records[0],
52                     me = this;
53                 
54                 me.getBookSideBar().getSelectionModel().select(record);
55             }
56         }, 500, this);
57     },
58     
59     /**
60      * Shows a specified record by binding it to
61      */
62     showBook: function(record) {
63         var me = this;
64         
65         me.getBookView().bind(record);
66         me.getReviewList().bind(record, me.getReviewsStore());
67     }
68 });