Skip to content

Commit

Permalink
v0.1.2
Browse files Browse the repository at this point in the history
* added Hitch.App.prototype.redirect method
* added additional tests for Hitch.App
* fixed a bug in Hitch.App.prototype.load to not count `_autoLoad: false` flagged resources
  • Loading branch information
phillies2k committed Dec 11, 2012
1 parent 4cad423 commit ee049db
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .gitignore
@@ -1,5 +1,4 @@
node_modules
example
.npmignore
.idea
CHANGES.md
.idea
8 changes: 8 additions & 0 deletions CHANGES.md
@@ -0,0 +1,8 @@
CHANGES
=======


### 0.1.2
* added Hitch.App.prototype.redirect method
* added additional tests for Hitch.App
* fixed a bug in Hitch.App.prototype.load to not count `_autoLoad: false` flagged resources
3 changes: 2 additions & 1 deletion README.md
@@ -1,5 +1,6 @@
Hitch v0.1.1
Hitch v0.1.2
==================
@see: [CHANGES](Hitch/tree/master/CHANGES.md)

Lightweight backbone-based single page application framework.

Expand Down
62 changes: 56 additions & 6 deletions hitch.js
@@ -1,10 +1,10 @@
/**
* Hitch.js - v0.1.1
* Hitch.js - v0.1.2
* Lightweight backbone based single page application framework
*
* @author: Philipp Boes <mostgreedy@gmail.com>
* @copyright: (c) 2012 Philipp Boes
* @version: 0.1.1
* @version: 0.1.2
*
*/
(function() {
Expand All @@ -24,7 +24,7 @@
extend = Backbone.Router.extend;

// keep in sync with package.json
Hitch.VERSION = '0.1.1';
Hitch.VERSION = '0.1.2';

/**
* Hitch.Access Mixin
Expand Down Expand Up @@ -1193,26 +1193,64 @@
}
},

/**
* redirects to another location
* @param module
* @param action
* @param params
*/
redirect: function(module, action, params) {

var modulePropertyName = _.lcFirst(module) + 'Router'
, routes;

params = params || {};

if (!this[modulePropertyName]) {
throw new Error("no module found with that name: " + module);
}

routes = this[modulePropertyName].routes;

_.each(routes, function(actionName, route) {
if (action === actionName) {

_.each(params, function(value, key) {
route = route.replace(new RegExp(':' + key, 'g'), function(m) {
return value;
});
});

this.navigate(route, true);
}
}, this);
},

/**
* Loads all resources and fires a ready event when all resources are in sync with the server.
* @param resources
*/
load: function(resources) {

var length
, loaded = 0;
, loaded = 0
, queue = [];

if (!_.isArray(resources)) {
resources = _.toArray(arguments);
}

length = resources.length;
queue = _.filter(resources, function(resource) {
return resource._autoLoad === true;
});

length = queue.length;
this.resources = {};

if (!length) {
this.trigger('ready', this.resources);
} else {
_.each(resources, function(resource) {
_.each(queue, function(resource) {

if (_.isFunction(resource)) {
resource = new resource();
Expand Down Expand Up @@ -1266,18 +1304,30 @@
}
},

/**
* called on router initialization
*/
initialize: function() {
// overwrite in subclasses
},

/**
* called on ready event
*/
ready: function() {
// overwrite in subclasses
},

/**
* default home route callback
*/
index: function() {
// overwrite in subclasses (default home route)
},

/**
* default error route callback
*/
error: function() {
// overwrite in subclasses (default error route)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "backbone-hitch",
"version": "0.1.1",
"version": "0.1.2",
"description": "Lightweight framework built on top of backbone",
"keywords": [
"framework",
Expand Down
24 changes: 23 additions & 1 deletion test/tests.js
Expand Up @@ -199,6 +199,9 @@
}
});

/**
* @covers Hitch.App.prototype.setName
*/
test('setName', function() {

var name = 'testname'
Expand All @@ -212,7 +215,10 @@

});

asyncTest('load', function() {
/**
* @covers Hitch.App.prototype.load
*/
asyncTest('load triggers ready event', function() {

var UsersRepository = Hitch.Resource.extend({ url: 'mock.json', model: Hitch.User, parse: function(response) { return _.values(response); } })
, users = new UsersRepository();
Expand All @@ -225,6 +231,22 @@
app.load(users);
});

/**
* @covers Hitch.App.prototype.load
*/
asyncTest('loading resources', function() {

var UsersRepository = Hitch.Resource.extend({ url: 'mock.json', name: 'users', model: Hitch.User, parse: function(response) { return _.values(response); } })
, users = new UsersRepository({ load: true });

app.on('ready', function(resources) {
deepEqual(resources.users, users, 'resource were loaded');
start();
});

app.load(users);
});


}).call(this);

Expand Down

0 comments on commit ee049db

Please sign in to comment.