Showing posts with label Spine. Show all posts
Showing posts with label Spine. Show all posts

Thursday, February 9, 2012

iOS App Development with PhoneGap and Spine Mobile: Coffee Shop App

After looking at several mobile application frameworks I’ve finally decided to use Spine Mobile and PhoneGap to build an application.


Most first applications should be tossed out. I'm learning new stuff and whatever I create in this first application is guaranteed to have, at best, a whiff of code smell (if not reek of it). I don’t want to be saddled with an application that will most likely be mangled by the time I’m done. It’s a proof of concept prototype, or at least it should be. This also takes away the pressure to make it perfect.


I need something more than a sample application, but nothing too complex. It should be something I can start small and build on while still being somewhat useful. I’d like it to perform at least the following functions:
  • CRUD operations
  • Navigation between modules
  • Local data storage
  • Auto-magic data updates from the "cloud"
  • Some form of membership or identity
I think a simple coffee shop application should fit the bill. It can start out small, a simple menu for users to browse what’s available. Then I’ll add features as I go.


Version 0.1

  • A home page with a logo
  • A menu of available beverages
  • Location and hours of operation information

Version 0.2

  • A list of “my favorite” beverages
  • Loyalty punch card
  • Build a beverage (size, to go, flavors, etc.) 

Version 0.3

  • Order a beverage and update my loyalty card

Version 0.4

  • Coupons and/or promotions
  • Management of coupons and/or promotions
and so on...


Wednesday, February 1, 2012

JavaScript Frameworks: Still looking...

So many choices!

There are so many MV* frameworks and libraries available for client side JavaScript application development it’s nearly impossible to give them the proper amount of time to investigate them.

I’ve looked at Backbone.js, Knockout.js, Spine.js, Ember.js, Zappa, Express, JavaScriptMVC, Require.js, jQuery, jQuery Mobile, Handlebars.js, Jasmine, Angular.js, the list goes on.

For the server side I’m looking at Node.js and possibly MongoDB or CouchDB. Not sure…

I’ve also learned just how much I don’t know about JavaScript and how far it’s come. I had no idea what a REPL was until someone mentioned jsFiddle on Twitter.

Then there’s the whole JavaScript or CoffeeScript thing. I think it’s important to learn JavaScript before jumping into CoffeeScript, but it sure looks inviting…

At the moment, entirely subject to change, it looks like either Knockout.js or Backbone.js. I like the MVVM-like nature of Knockout.js data binding. But, Backbone.js has a very active community behind it. I’m nearing the point where I’ll just have to bite the bullet and pick one or I’ll never get going.

More examples soon. I promise!

I’m cursed, but I love it. May you live in interesting times or something like that…

Sunday, December 4, 2011

iOS App Development with PhoneGap and Spine Mobile: iOS Example App

In my prior post, I made a simple modification to Alex MacCaw's mobile contacts example. Today I'll walk through the steps to wrap it with PhoneGap as an iOS application.

Detailed directions are available for setting up PhoneGap for iOS using Xcode. You'll also want to read Alex MacCaw's documentation on integrating Spine Mobile with PhoneGap. It's in the early stages of development so there is a bit of manual tweaking necessary.

Setup an Initial PhoneGap Project

Open a terminal and change to your spine mobile application directory. 

$ cd ~/Documents/spine.mobile.contacts
Create a subdirectory for your Xcode project. Alex suggests using ./build/ios within your spine project directory (e.g. ~/Documents/spine.mobile.contacts/build/ios).
Start Xcode and select the PhoneGap-based application template.



Enter your Product Name and Company Identifier. I chose MyContacts. Not very original, eh? 



Change the Scheme to iPhone 5.0 Simulator and run the application. You'll get a warning indicating the www folder is missing from your project and the iPhone Simulator will display an error message that index.html was not found.





Quit the iOS Simulator. Drag the www folder created in the project directory onto the project folder in Xcode. This will add a reference to it in your Xcode project. 




In the dialog box that pops up, make sure you check the Create folder references for any added folders option.




Run the project again and you'll get a default sample PhoneGap application with an alert in the iOS Simulator. It works!





Tweak your Spine Mobile Application for PhoneGap

You need to add phonegap.js to your spine mobile application. The current version is named phonegap-1.2.0.js. Make a new directory in the spine project directory called lib (~/Documents/spine.mobile.contacts/lib). Copy phonegap.js from the www directory (~/Documents/spine.mobile.contacts/build/ios/MyContacts/www) into the new lib directory. You also need to add phonegap.js to the libs section of the slug.json file.

{
  "dependencies": [
    "es5-shimify",
    "json2ify",
    "jqueryify",
    "gfx",
    "spine",
    "spine/lib/local",
    "spine/lib/ajax",
    "spine/lib/route",
    "spine/lib/tmpl",
    "spine/lib/manager",
    "spine.mobile"
  ],
  "libs": [
    "./lib/phonegap-1.2.0.js"
  ]
}

I also needed to modify the spine.mobile.contacts index.html page slightly. Change href="/application.css" and src="/application.js" to href="./application.css" and src="./application.js".

<!DOCTYPE html>
<html>
<head>
  <meta charset=utf-8>
  <title>App</title>
  <link rel="stylesheet" href="./application.css" type="text/css" charset="utf-8">
  <script src="./application.js" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript" charset="utf-8">
    var jQuery  = require("jqueryify");
    var exports = this;
    jQuery(function(){
      var App = require("index");
      exports.app = new App({el: $("body")});
    });
  </script>
  <meta name="viewport" content="width=device-width; initial-scale=1.0; minimum-scale=1; maximum-scale=1.0; user-scalable=0;"/>
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="format-detection" content="telephone=no" />
</head>
<body>
</body>
</html>

Compile your application with hem from the spine project directory.$ hem build
At this point you could test your spine mobile application using a browser. Just run hem server and browse to http://localhost:9294/. You can even use your iPhone/iPod Touch if your computer firewall will allow it. Run a command like ifconfig or look in your network preferences for your ip address (e.g. http://192.168.1.112:9294).

$ hem server

You need to copy the contents of the spine mobile project public directory (~/Documents/spine.mobile.contacts/public) to the www directory in the Xcode project (~/Documents/spine.mobile.contacts/build/ios/MyContacts/www). You'll need to do this after each hem build. I added a step to the build phase to automate this. You could also add a command to build your spine mobile project before it's copied. For now, just copy application.css, application.js, cache.manifest, favicon.ico, and index.html manually. 

Run the Xcode project again... Voila!

You now have a working iOS application. Future posts will include setting up icons, syncing data with a server, etc.










Tuesday, November 22, 2011

iOS App Development with PhoneGap and Spine Mobile: Example App



Alex MacCaw, the creator of Spine and author of O'Reilly's JavaScript Web Applications, has a simple example of a mobile contact application. We'll set this application up, add a little functionality, and eventually wrap it in PhoneGap as a iOS app for the iPhone.

There's an explanation on spinejs.com you can follow for a thorough explanation of the application. 


Let's get it setup and running locally.
  1. Download the app from github.com. My copy is under ~/Documents/spine.mobile.contacts/.
  2. Open a terminal and change to the spine.mobile.contacts directory.
    $ cd ~/Documents/spine.mobile.contacts
  3. Install the local npm modules
    $ npm install .
  4. Start the hem server
    $ hem server
  5. Browse to the application at http://localhost:9294/
You should now see this...


Go ahead and add a few contacts. HTML5 local storage is used to persist the data.

Now, let's add a way to delete contacts. Open contacts.coffee located in the ~/Documents/spine.mobile.contacts/app/controllers/ directory with a text editor. We want to add a delete button to the ContactsShow class. ContactsShow is the controller for the panel that displays an individual contact when you click on a contact in the list view.

The changes are pretty simple.

class ContactsShow extends Panel
  className: 'contacts showView'

  constructor: ->
    super

    Contact.bind 'change', @render

    @active @change

    @addButton('Back', @back)
  
    # Delete button placed to the right with a callback 
    # to a delete function
    @addButton('Delete', @delete).addClass('right')

  render: =>
    return unless @item
    @html require('views/contacts/show')(@item)

  change: (params) ->
    @item = Contact.find(params.id)
    @render()

  # Delete callback function
  # Confirm the delete, then navigate back to the list view
  # If they change their mind, stay here
  delete: (params) ->
    if confirm('Are you sure?')
      @item.destroy() 
      @navigate('/contacts', trans: 'left')

  back: ->
    @navigate('/contacts', trans: 'left')

Save your changes. There's no need to restart the hem server. Just reload the site in the browser, hem dynamically picks up the changes with each request.

Click on a contact, the item view displays with the delete button on the top right.

Click the delete button. You should see a confirmation dialog...
Click cancel and you'll see the item view. Click ok, the contact is deleted and the list view is redisplayed.

In my next blog post I'll finish wrapping the app in PhoneGap.


Sunday, November 20, 2011

iOS App Development with PhoneGap and Spine Mobile: Setup

You need to setup your development environment. 


Since I'm targeting iOS I'm obviously using a Mac with OSX Lion and Xcode 4.2. You can find help with installing Xcode at developer.apple.com. Done? Good.



Here's a summary of the setup. The various web sites have more details.

Install Homebrew
If you're familiar with Linux's apt-get you'll love Homebrew. Simplifies installation and update of the Unix-y bits and bytes for OS X. Homebrew installs to the user's local space so sudo isn't required.

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

Install node.js via brew

$ brew install node

Install npm
For some reason, npm has been removed from brew. Use curl to download and install it.

$ curl http://npmjs.org/install.sh | sh
Install Spine, Spine.app, and Hem via npm


$ npm install -g spine spine.app hem


iOS App Development with PhoneGap and Spine Mobile: Introduction

A friend of mine recently asked me if I knew how to write iOS applications. She has an idea for an app (don't we all). Well, iOS development is something I've wanted to do since I picked up an iPod Touch a couple of years ago, I just haven't made the time to do it. 

I recently started learning Node.js and the Spine MVC framework for HTML5 web application development. The last website I worked on was several years ago (ASP.Net 1.1) and I want to try something that's not in the Microsoft stack. I've done PHP in the past but didn't really want to try it again. Javascript is hot and CoffeeScript has a lot of bleeding-edge attention right now. Maybe I can get ahead of the curve.

Spine also has a mobile framework that supports HTML5 web sites targeting mobile devices. I finally got a on of the simple demo apps working this week. Pretty darn easy once the sample app and the current Spine framework were in sync. 

Once I got that working I noticed a doc describing integration between Spine and PhoneGap. PhoneGap wraps your HTML5/JavaScript app into a mobile device native application with access to the device API. You can also deploy the application to multiple platforms. It's also open source. Sounds winning to me.

In future posts I will describe how to develop an iOS native application using PhoneGap, Spine Mobile, and CoffeeScript. Since the frameworks are constantly changing, I intend to keep an up to date working copy.