The Idea

Sparky.js is a client-side application scaffold which helps those who want to have organized structure in their app, but don't want to subscribe to a particular client-side MVC framework.

It provides such capabilities as a code Router, an Event system, an Ajax wrapper, and more.

Say goodbye to spaghetti JS and hello to SparkyJS


					

FAQ

Is this a client side framework?

No. SparkyJS is not a framework, but an "application template" to bootstrap your client-side code with.

Does this work with frameworks like Backbone or Ember?

SparkyJS provides structure to applications that do not use a framework such as Backbone or Ember. Elsewise, those frameworks should provide you with all the structure you'd need.

When would I need to use Sparky?

If you're developing a web app, a small-medium sized website, or even a Wordpress theme, SparkyJS can be used. The goal is the DRY concept, to simplify the process of structuring your code, and eliminate redundant code execution on pages that do not need it.

How do I get started?

Sparky is fast to get working with, lightweight and simple to understand. First Customize your build to what you would need, then include jQuery and your downloaded SparkyJS file. Begin writing your logic in the App object, Events.endpoints object, and your Routes object.

FAQ

I wish SparkyJS were more modular. Can I use RequireJS?

Any library can be used in conjunction with SparkyJS, be it jQuery, RequireJS, or MooTools. It is nothing more than a logically organized structure in which you base your application upon.

What is the Routing feature?

The Routing feature of SparkyJS is used to bind code to specific pages of your website, it does not actually handle any URL routing. The best way of doing this is outputting a Controller and an Action in your head meta, but that data can be replaced with whatever you want.

For example: If you were developing custom wordpress theme, you can output the template name as the Controller and the page/post type as the Action, making it easy to bind your code based off of your pages requirements.

Does my site need to be dynamic to use Routing?

Absolutely not (though it is recommended). You can physically write in your own Action and Controller in any pages you want, and it will bind it to the appropriate objects.

FAQ

Why would you want to bind events inline?

Having the ability to minimize the amount of event-bindings in your code is great, leaving you to focus on what is important: the logic. Need to add support for a number-only text field? Easy, add it via data-attributes as you write your code. Options are endless

Can I still bind events through selectors?

You are free to pick and choose what you like about SparkyJS, and trash the rest. Don't need to bind events inline? Trash it. Don't need an Ajax wrapper? Trash it. Don't need routing? Trash it. Sparky is meant to work for you and should not be used without tailoring it to your needs.

Any last words?

SparkyJS was created to solve a problem. There are many different methods of structuring your javascript-applications, and this is one of them that has helped me regain my sanity. If you enjoy structure and organization on your server side code, why should your client side code settle for anything less? Enjoy!

Basics

To fully utilize Sparky's routing, a few things must be done outside of the script.

In your application, output the meta tags you see to the right with their respective content dynamically filled in with their appropriate values.

Prepend the name of your meta attributes with "app-". Sparky will retrieve those values during initialization and save them as settings. To fully utilize Sparkys Ajax, Utils, and Routing, it is recommended you use app-url and app-route with their respective values.

Include jQuery, and the path to your Sparky application, and you're off.


					

Utils

Now that your meta tags are set, lets explore what Sparky does. The Utils object is used for caching objects, outputting dynamic data, defining app settings, and routing your application.

Upon calling Utils.settings.init(), the object Utils.settings will be filled with the data that was output from the meta tags (excluding the app- text that was prepended to the names.).

Utils.cache is good for storing references to whatever it is you may be accessing frequently

After initializing your apps settings, calling Utils.home_url('auth/login?foo=bar') would return http://myapp.com/auth/login?foo=bar.

The function Utils.log is a wrapper for console.log, so that way you may turn logging on/off via the debug variable in the settings object. The log function is made more accessible by assigning the variable _log to it. Making a _log('is easy');.

A new addition to Sparky.js is ParseObject.js, a simple but powerful way to test against an objects existence from a string with any type of unique delimiters. The parseRoute function is used for Events and Routing.

Lastly, we have the Utils.route function. Note: This is not to be confused with the Routes object. This function will extract our route as defined in our settings, and see if a relative object exists within the Ruotes object.


					

Routing

Routing is a big part of Sparky's charm. It is very easy to execute your page-specific logic in an organized fashion, making management of your app a breeze. With a JS loader like RequireJS, you can develop dynamic, structured, and modular apps in conjunction with Sparky's architecture.

Lastly, we have the Utils.route function. Note: This is not to be confused with the Routes object. This function will extract our route as defined in our settings, and see if a relative object exists within the Ruotes object.

The following routes would produce the following values if they were called in conjunction with a Route object to the right:

  • foo/bar: "bar function"
  • sample/any: "" (any is not a function)
  • sample/any/depth/allowed: "depth init function", "allowed function"

If your route exists, and if there were any functions named init within your object literals, they will be qued and then called in the order they were read, allowing you to apply global logic to higher routes while trickling down to fine tune your code.


					

Ajax

Though the process of Ajax is simplified quite well with libraries like jQuery, it is beneficial to use a wrapper to avoid writing all the requests and simplify your life.

The base Ajax Object provided in Sparky allows a simple way to make your request. Ajax.call for POST requests, and Ajax.get for get requests.

A request for a "Like Button" might look like this:

Ajax.call('doLike', {pictureID: 7}, function(result){
	// Success
});

First parameter would be the method we want to call for our Ajax script, second parameter is the data, and the third is the success function. Using a wrapper is beneficial, for example: You could output app-userid in your meta, and determine whether or not a user is logged in before they even make an ajax request.


					

Events

Events play an important role in Javascript and the DOM. Javasript should be used to handle the logic, but having the ability to bind events by writing HTML is a breeze, and better represents what your specific elements can do. Say goodbye to the days of messy and disorganized event binding!

Firstly, create a few HTML elements in which you want to bind events to. Using HTML5 data attributes, assigning data-event to an element will bind it to the function name of its value. For instance: data-event="foo.bar" will bind it to Events.endpoints.foo.bar function if it exists.

Events, unlike routes, are delimited by a dot . but can be changed to whatever you'd like.

The default event type bound to the element is the click event. If you wish to listen for a different type of event, add the data-attribute data-method to your element and it's value should be it's respective event type.

If you add to the DOM and want to bind elements, you may call Events.init() or Events.bindEvents(). Elements that have already been bound will not be bound again, thus binding only new elements.

Provided are a few examples of the implementation of events. The this keyword is available, and if you need to inspect the event object, you may pass it as a parameter to your endpoint function.


                        

    					

                        

                   	

App & Public

The App object is used to start Sparky, bind events, and route your code. It can also house the core of your logic if you wish. Usually, it is best to put any global logic in the App object.

The Public object is returned to allow you to publicize any functions you wish, elsewise, the scope of your functionality will be within your application itself. The App.init function is returned by default.


					

Getting Started

Sparky helps you organize and keep your code under control, but there is so much to Sparky! Where should you put your code? What code is necessary? Where does my app's logic lay?

If you have questions about how to get started with Sparky, make a simple todo list for yourself when starting up a new project:

  • 1. Output your meta tags, preferably the tags who's name is app-url and app-route.
  • 2. Create your routes objects respectively to the routes output in your head, and if you prefer, link them to a function within App.logic
  • 3. Create some elements with events and fill your Events.endpoints object with juicy events.
  • 4. Add an optional object within App.logic called global, and call it before your Utils.route() function in the App.init() function to apply global page logic.
  • 5. Extend Extend Extend! Modify the functions provided in Sparky until they fit your needs perfectly.


					

Customize

Name your Application
Feature Set
Sample Logs

An idea by @jakiestfu.
Etiquette and Critiquing by @_jayd3e

Github Repo

It is recommended you take a peek at the Sparky.js GitHub repo to see demos and additional variations of Sparky.js

View on GitHub

					

Discuss

Fork me on GitHub