baseJS: A Mobile JavaScript Framework
- Date
- 16.02.09
- Comments
- 7
This past fall I was tasked with creating a few iPhone-specific web applications. After doing a lot of research, I found a few issues with the popular JavaScript libraries. The availability of -webkit-transform and -webkit-animation removed any need for an animation library. Second, since the Query Selectors API was available, the need for a complex javascript selectors engine was basically pointless1. And last, every library that I came across, even after minifying, was far too large for the iPhone to cache (24KB limit).
So looking at these first two issues, I tend to feel like those, as well as cross-platform issues, are some of the biggest reasons we use JavaScript frameworks in the first place. But here I was, developing for a single platform. So, what did I do?
I wrote baseJS. I just needed a simple, lightweight framework that could take care of some common and bulky tasks and allow me to write cleaner, more organized code.
Features
- Lightweight: less than 8KB when minified
- Fully-compatible with Mobile Safari
- Falls back on Sizzle selector library for iPhone 1.x (when query selector support is unavailable)
- Encourages use of CSS Transitions and Animations
- Object-oriented
- Easy to extend
Quick Examples
Organization Syntax of Classes
var AppClass = function() {};
base.extend(AppClass.prototype, {
method1: function() { return true; },
method2: function() { return false; }
});
Is equivalent to:
var AppClass = function() {};
AppClass.prototype.method1 = function() { return true; };
AppClass.prototype.method2 = function() { return false; };
Element Selectors
baseJS uses the Selectors API if available, or falls back on the Sizzle selector library if query selectors are not available.
var header = $('#header')[0]; // Selects the first object on the page with ID 'header'
var awesome = $('div.awesome', header); // Selects every instance of <div class="awesome"></div> in #header
Ajax Shortcuts
Face it: writing AJAX is tedious and requires a lot of steps. baseJS’s io() class simplifies the process.
new io('/results.json', {
format: 'json',
method: 'post',
params: { mustache: true },
onSuccess: function(response) {
alert(response); // response == data returned from server
},
onFailure: function() {
alert('There was an error getting the data');
}
});
Element Creation Shortcuts
var newLink = new Element('a', { class: 'topLink', href: '#top' }, 'Back to Top');
$('#foo').appendChild(newLink);
Appends the following to <div id="foo"></div>:
<a class="topLink" href="#top">Back to Top</a>
There’s More
Be sure to check out the baseJS API and Documentation to see a full list of included methods and get help using them.
Get baseJS 1.0rc1
baseJS is nearing 1.0-complete status. You can currently nab the first release candidate and read the documentation to get you started.
Get Involved with baseJS
baseJS relies on your help to make it as awesome as possible. Feel free to fork it at GitHub and report bugs via Lighthouse
License
baseJS is licensed under the GNU General Public License v3.0. More information is available in the download.
Footnotes
1That is, unless you need iPhone 1.0 compatibility, but don’t worry: Sizzle has been included as an optional fallback.
7 Comments
I ain't no programmer, but I dig the logo. Keep it up Paul.
You're going public with base.js? Nice work man! I like the name of your AJAX class too.
We've used baseJS for several high-profile iPhone web projects already, and it's really improved our productivity. The code is clear, concise, easy to understand, and easy to extend. I can imagine it will only get better now, with more eyes on it.
Thanks, Phil and Eric. It's unfortunate that the projects I originally intended baseJS for are NDA and I can't show them off. It's good to know that you all are still finding it useful.
I'm also planning on running up some posts on best-practices and uses for baseJS, showing off the power of its structuring and extendability.
I have started a project (www.jqtouch.com) which is a jQuery-based JS framework for iPhone UI. I don't believe we're in any competition, but could probably help each other. You may be interested in including native animations, which I have a start at -- I could also see switching from jQuery as my base library to save weight. Anyway, just wanted to make the introduction. Thanks-
Interesting way to create a class ...
Now If I create a class Tree as described in your example...
var Tree = function() {};
base.extend(Tree.prototype, {
method1: function() { return true; },
method2: function() { return false; }
});
And now to create a Class "Oak" that inherits method1 and method2 and adds a third method would look like this ?
var Oak= function() {};
base.extend(Tree.prototype, {
method3: function() { return "huh"; }
});
Or is that wishful thinking ?
Umpf ... taking foot outta my mouth ...
Of course if I had re-read the documentation, I would have figured out that it is not so ...however, I am using this to do it:
http://javascript.crockford.com/inheritance.html#sugar