MVC for JqueryMobile
After I saw a demo using knockoutjs for jquery mobile, I realized that it is not as clean as when it is used in a javascript application. Reading this book, I took the initiative to redo the last chapter’s example and apply MVC instead. I bumped several times with a few issues but overall it was very clean and easy to implement.
Complains:
- I dislike the fact that having all pages in a single file do not support url parameters passed from one page to another. This is not the case when creating pages in separate files. Visit this gallery and you will notice stores go this route giving customers a way to reference the url for a given shopping item.
Discoveries:
- Initially I was setting each page control to listen to its view’s page events. I realized the main page was being created earlier for its control to catch the pageshow event. I switched to read from document and using the observer pattern each control is called for its page event notifications. (app/control/PageWatcher.js)
- I created a few utilities such as a method to return reference of header/content/footer for a given page. This helped in order to reference each time inside controls.(app/utils/PageUtils.js)
- I used my own namespace object “app” and in this way my application is sealed from global variables and the dom.
- Having my namespace object i used it as the target event for one control to listen another one. ( ex: app/control/TweetDetail.js line 29 )
- This was the first time I set event names as constants which took me back to PureMVC’s approach. (app/Ev.js)
Good practices:
- Turn on and off when listening to changes within each page content comes to be shown and then hidden
- Best is for data such as tweets from this demo to be stored in javascript objects rather than saving them as data in html elements.
This demo source code can be found at github and you can see the application in here







