SenchaTouch becomes more readable and easier with Coffeescript

I always thought SenchaTouch is a fantastic mobile framework. But it quickly becomes difficult to code, especially if you do not master completely.

Fortunately, the birth of Coffeescript really changed the game.

This post is just a quick sample source code to illustrate my point. This a simple “splitview” sample. I just want to see a list of choices, allowing me to view multiple screens.

Something like that :

Alt "sencha-coffee.png"

Initialize the html page :

Before, you need (of course) :

Then, create an html page :

<!DOCTYPE html>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
		<link rel="stylesheet" href="sencha/sencha-touch.css" type="text/css">
		<script src="coffee-script.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="sencha/sencha-touch.js"></script>
	<script src = "" type="text/coffeescript"></script>


And, now, create a file in which we will enter the code that follows.

First, “Model & Models”

Define them

class Model extends Ext.regModel
	constructor:(name, fields)->
		super name, 
			fields : fields
		@name = name

class Models extends
	constructor:(model, sorters, data)->
			model :
			sorters : sorters
			data : data

Initialize “Model & Models”

menuChoice = new Model 'menuChoice', ['code', 'label', 'item']

menu = new Models menuChoice, 
		(code : '01', label : 'Card One',   item : 0)
        (code : '02', label : 'Card Two',   item : 1)
		(code : '03', label : 'Card Three', item : 2)

Next, UI Components

class Header extends Ext.Toolbar
			title : title
			dock : 'top'

class Footer extends Ext.Toolbar
			title : title
			dock : 'bottom'

Card (the screen that appears on the right side)

class Card extends Ext.Panel
			margin : '10px'
			scroll : 'vertical'
			height : '100%'
			style  : 'background-color:white;'
			html   : html

Right split view (a Card container)

class RightView extends Ext.Carousel
			height : '100%'
			layout : 'fit'
			direction : 'vertical'
			style : 'background-color:white;'
			items : cards #  this is an array of cards

Left split view (a kind of menu or list)

class LeftView extends Ext.List
	constructor:(template, models, linkedView, fieldItemIndex)->
			scroll : 'vertical'
			dock : 'left'
			style : "border-right:solid black 1px;"
			width : 250
			itemTpl : template
			store : models
			listeners :
				itemtap : (subList, subIdx)->
					store = subList.getStore()
					record = store.getAt subIdx
					# When i "tap" a item menu, i activate the corresponding card
					linkedView.setActiveItem record.get fieldItemIndex

The main screen (it will encapsulate all the elements of the UI)

class MainScreen extends Ext.Panel
			fullscreen : true
			dockedItems: [items.header,items.footer,items.leftSidePanel]
			items : [items.rightSidePanel]

Last step : initialize the application

	onReady: ->
		header = new Header "Sencha <3 Coffeescript"
		footer = new Footer "by k33g_org"
		card1 = new Card "
			<b>Card 1</b>
				Donec sed odio dui. Maecenas sed diam eget risus varius blandit sit amet non magna. 
		card2 = new Card "
			<b>Card 2</b>
				Morbi leo risus, porta ac consectetur ac, vestibulum at eros. 
		card3 = new Card "
			<b>Card 3</b>
				Maecenas sed diam eget risus varius blandit sit amet non magna. 

		rightView = new RightView [card1, card2, card3]
		leftView = new LeftView '{code} {label}', menu, rightView, 'item'
		mainScreen = new MainScreen 
			header : header 
			footer : footer
			leftSidePanel : leftView
			rightSidePanel : rightView

That’s all. Launch it !

If you don’t know SenchaTouch, try to do it in javascript … ;)

blog comments powered by Disqus

Related posts