Skip to content
Browse files

* update to v0.1.0 RC-1

  • Loading branch information...
1 parent 42c5569 commit e10e4f764ffa88eb82b1e5ece650d59f4360a4b5 phillies2k committed Dec 9, 2012
Showing with 116 additions and 401 deletions.
  1. +2 −1 .gitignore
  2. +4 −5 README.md
  3. +1 −1 bin/hitch
  4. +41 −306 hitch.js
  5. +1 −1 lib/console_utils.js
  6. +26 −0 lib/data/build.ejs
  7. +6 −17 lib/data/config.ejs
  8. +0 −15 lib/data/hitch.json.ejs
  9. +3 −3 lib/data/index.ejs
  10. +1 −2 lib/data/main.ejs
  11. +30 −14 lib/functions.js
  12. +1 −1 package.json
  13. +0 −35 test/tests.js
View
3 .gitignore
@@ -1,3 +1,4 @@
node_modules
.npmignore
-.idea
+.idea
+CHANGES.md
View
9 README.md
@@ -1,8 +1,7 @@
-Hitch v0.1.0-alpha
-=====
-Lightweight backbone-based single page application framework.
+Hitch v0.1.0
+==================
-***Hitch is currently in a very early alpha state.***
+Lightweight backbone-based single page application framework.
* Default [Hitch.Object](#hitchobject)s for [users](#hitchuser), [credentials](#hitchcredentials) and [roles](#hitchrole)
* Powerful [Access Control Layer](#hitchacl) for controlling access to [Hitch.Router](#hitchobject)s
@@ -239,4 +238,4 @@ The enhanced Backbone.Router
A default user model representation
### Hitch.View
-The enhanced Backbone.View
+The enhanced Backbone.View
View
2 bin/hitch
@@ -6,7 +6,7 @@
*
* @author: Philipp Boes <mostgreedy@gmail.com>
* @copyright: (c) 2012 Philipp Boes
- * @version: 0.1.0-alpha
+ * @version: 0.1.0
*/
var buildTarget = require('../lib/functions').buildTarget
View
347 hitch.js
@@ -1,10 +1,10 @@
/**
- * Hitch.js - v0.1.0-alpha
+ * Hitch.js - v0.1.0
* Lightweight backbone based single page application framework
*
* @author: Philipp Boes <mostgreedy@gmail.com>
* @copyright: (c) 2012 Philipp Boes
- * @version: 0.1.0-alpha
+ * @version: 0.1.0
*
*/
(function() {
@@ -24,7 +24,7 @@
extend = Backbone.Router.extend;
// keep in sync with package.json
- Hitch.VERSION = '0.1.0-alpha';
+ Hitch.VERSION = '0.1.0';
/**
* Hitch.Access Mixin
@@ -44,7 +44,7 @@
this.acl = new Hitch.ACL();
}
- return this.acl
+ return this.acl;
},
/**
@@ -62,7 +62,7 @@
};
/**
- * Hitch.Cookies Mixin
+ * Hitch.Cookies
* @type {Object}
*/
Hitch.Cookies = {
@@ -294,21 +294,27 @@
_.mixin(Hitch.Helpers);
/**
- * Hitch.AccessControlList
+ * Hitch.ACL
* @constructor
*/
- Hitch.AccessControlList = function() {
+ Hitch.ACL = function(obj, permissions) {
+
this._publicPermissions = {};
this._routePermissions = {};
+
+ if ($.isPlainObject(obj) && obj.public && obj.routes) {
+ this._publicPermissions = obj.public;
+ this._routePermissions = obj.routes;
+ }
};
/**
- * Hitch.AccessControlList.prototype
+ * Hitch.ACL.prototype
* @type {Object}
*/
- Hitch.AccessControlList.prototype = {
+ Hitch.ACL.prototype = {
- constructor: Hitch.AccessControlList,
+ constructor: Hitch.ACL,
getAccess: function(route, obj) {
@@ -344,241 +350,23 @@
permissions = !!permissions;
}
- if (!this._routePermissions[route]) {
- this._routePermissions[route] = {};
- }
-
- this._routePermissions[route][obj.id] = permissions;
- }
- };
-
- /**
- * Hitch.ACL
- * @param permissions
- * @constructor
- */
- Hitch.ACL = function(permissions) {
-
- this.permissions = {};
-
- if (permissions instanceof Hitch.Object || permissions instanceof Hitch.Resource) {
-
- this.setReadAccess(permissions, true);
- this.setWriteAccess(permissions, true);
-
- } else if (_.isObject(permissions)) {
-
- _.each(permissions, function(accessList, userId) {
+ if (route === '*') {
- this.permissions[userId] = {};
+ this._publicPermissions[route] = permissions;
- _.each(accessList, function(allowed, permission) {
- this.permissions[userId][permission] = allowed;
- }, this);
-
- }, this);
-
- }
- };
-
- // Public access role
- Hitch.ACL.PUBLIC = 'PUBLIC';
-
- /**
- * Hitch.ACL.prototype
- * @type {Object}
- */
- Hitch.ACL.prototype = {
-
- constructor: Hitch.ACL,
-
- /**
- * Returns the user id
- * @param userId
- * @return {*}
- * @private
- */
- _determineUserId: function(userId) {
- if (userId instanceof Hitch.User) {
- return userId.id;
- } else if (userId instanceof Hitch.Role) {
- return 'role:' + userId.getName();
- } else if (userId instanceof Hitch.Resource) {
- return 'resource:' + userId.name;
- } else if (_.isObject(userId)) {
- return userId.toString();
} else {
- return userId;
- }
- },
-
- /**
- * Returns access permissions for the given access type and user id
- * @param accessType
- * @param userId
- * @return {*}
- * @private
- */
- _getAccess: function(accessType, userId) {
-
- var permissions;
-
- userId = this._determineUserId(userId);
- permissions = this.permissions[userId];
-
- if (!permissions) {
- return false;
- }
-
- if (!permissions[accessType]) {
- return false;
- };
-
- return permissions[accessType];
- },
-
- /**
- * Sets the access rights for the given access type and user id
- * @param accessType
- * @param userId
- * @param allowed
- * @private
- */
- _setAccess: function(accessType, userId, allowed) {
-
- var permissions;
- userId = this._determineUserId(userId);
- permissions = this.permissions[userId];
-
- if (!permissions) {
- if (!allowed) return;
- permissions = {};
- this.permissions[userId] = permissions;
- }
-
- if (allowed) {
- this.permissions[userId][accessType] = true;
- } else {
- delete permissions[accessType];
- if (_.isEmpty(permissions)) {
- delete permissions[userId];
+ if (!this._routePermissions[route]) {
+ this._routePermissions[route] = {};
}
- }
- },
-
- /**
- * Returns the public read access permission
- * @return {*}
- */
- getPublicReadAccess: function() {
- return this._getAccess('read', Hitch.ACL.PUBLIC);
- },
-
- /**
- * Sets the public read access permission
- * @param allowed
- */
- setPublicReadAccess: function(allowed) {
- this._setAccess('read', Hitch.ACL.PUBLIC, allowed);
- },
-
- /**
- * Returns role read permissions
- * @param role
- * @return {*}
- */
- getRoleReadAccess: function(role) {
- return this._getAccess('read', role);
- },
- /**
- * Sets read access permissions for the given role
- * @param role
- * @param allowed
- */
- setRoleReadAccess: function(role, allowed) {
- this._setAccess('read', role, allowed);
- },
-
- /**
- * Returns the read access permission for a given user id
- * @param userId
- * @return {*}
- */
- getReadAccess: function(userId) {
- return this._getAccess('read', userId);
- },
-
- /**
- * Sets the read access permission for a given user id
- * @param userId
- * @param allowed
- */
- setReadAccess: function(userId, allowed) {
- this._setAccess('read', userId, allowed);
- },
-
- /**
- * Returns the write access permission for a given user id
- * @param userId
- * @return {*}
- */
- getWriteAccess: function(userId) {
- return this._getAccess('write', userId);
- },
-
- /**
- * Sets the write access permission for a given user id
- * @param userId
- * @param allowed
- */
- setWriteAccess: function(userId, allowed) {
- this._setAccess('write', userId, allowed);
- },
-
- /**
- * Returns the public write access permission
- * @return {*}
- */
- getPublicWriteAccess: function() {
- return this._getAccess('write', Hitch.ACL.PUBLIC);
- },
-
- /**
- * Sets the public write access permission
- * @param allowed
- */
- setPublicWriteAccess: function(allowed) {
- this._setAccess('write', Hitch.ACL.PUBLIC, allowed);
- },
-
- /**
- * Returns the write access permission for a given role
- * @param role
- * @return {*}
- */
- getRoleWriteAccess: function(role) {
- return this._getAccess('write', Hitch.ACL.PUBLIC);
- },
-
- /**
- * Sets the write access permission for a given role
- * @param role
- * @param allowed
- */
- setRoleWriteAccess: function(role, allowed) {
- this._setAccess('write', role, allowed);
+ this._routePermissions[route][obj.id] = permissions;
+ }
},
- /**
- * Returns a JSON representation of this ACL's permissions
- * @return {*}
- */
toJSON: function() {
- return _.clone(this.permissions);
+ return { public: this._publicPermissions, routes: this._routePermissions };
}
-
};
/**
@@ -763,6 +551,20 @@
}
return returnVal(model.get(attr), value, operator);
+ },
+
+ load: function(options) {
+
+ var success;
+
+ options = options || {};
+ success = options.success;
+
+ options.success = _.bind(function() {
+ this.trigger('load', this);
+ }, this);
+
+ return this.fetch(options);
}
}));
@@ -1065,19 +867,6 @@
// route filters
_filters: {},
- getCurrentUser: function(currentUser) {
-
- if (!this.currentUser) {
- if (currentUser instanceof Hitch.User) {
- this.currentUser = currentUser;
- } else {
- this.currentUser = new Hitch.User();
- }
- }
-
- return this.currentUser;
- },
-
/**
* Adds an after filter
* @param route
@@ -1119,11 +908,8 @@
var args = this._extractParameters(route, fragment)
, acl = this.getACL()
- , canAccessRoute = acl.getAccess(name);
-
- console.log(canAccessRoute, acl);
- if (!canAccessRoute) return;
+ if (!acl.getAccess(name)) return;
if (this._applyFilters('before', fragment, args)) {
callback && callback.apply(this, args);
@@ -1391,56 +1177,6 @@
},
/**
- * Returns the public interface for this application
- * @return {*}
- */
- getPublicInterface: function() {
- var publicInterfaceMethodNames = _.filter(_.keys(this), function(key) { return key.charAt(0) !== '_'; })
- return _.pick(this, publicInterfaceMethodNames);
- },
-
- /**
- * Returns the current session user
- * @return {Hitch.User}
- */
- getCurrentUser: function() {
-
- if (!this.currentUser) {
-
- var cookie = Hitch.Cookies.get('hitch-user')
- , self = this
- , user;
-
- if (cookie) {
-
- user = new Hitch.User(cookie);
- user.fetch({
-
- success: function(data) {
- user.set(data);
- self.currentUser = user;
- },
-
- error: function() {
- Hitch.Cookies.clear('hitch-user');
- user = new Hitch.User({ role: { name: 'visitor' } });
- Hitch.Cookies.set('hitch-user', user.id);
- }
-
- });
-
- } else {
- user = new Hitch.User({ role: { name: 'visitor' } });
- Hitch.Cookies.set('hitch-user', user.id);
- }
-
- this.currentUser = user;
- }
-
- return this.currentUser;
- },
-
- /**
* Appends an asset to the document head
* @param type
* @param source
@@ -1488,10 +1224,9 @@
resource.url = [ this.apiUrl, resource.name ].join('/');
}
- resource.fetch({
+ resource.load({
success: _.bind(function() {
this.resources[resource.name] = resource;
- resource.trigger('load', resource);
if (++loaded === length) {
this.trigger('ready', this.resources);
}
@@ -1522,7 +1257,7 @@
if (this.exports) {
var globalName = _.isString(this.exports) ? this.exports : this.name;
- root[globalName] = this.getPublicInterface();
+ root[globalName] = this;
}
},
@@ -1577,4 +1312,4 @@
}
};
-}).call(this);
+}).call(this);
View
2 lib/console_utils.js
@@ -54,4 +54,4 @@ ConsoleUtils.Command.prototype.accepts = function(args) {
return true;
};
-exports = module.exports = ConsoleUtils;
+exports = module.exports = ConsoleUtils;
View
26 lib/data/build.ejs
@@ -0,0 +1,26 @@
+({
+
+ baseUrl: "../public/js",
+
+ paths: {
+ "jquery": "vendor/jquery",
+ "underscore": "vendor/underscore",
+ "backbone": "vendor/backbone",
+ "hitch": "vendor/hitch",
+ "text": "vendor/text"
+ },
+
+ shim: {
+ "jquery": { exports: "$" },
+ "underscore": { exports: "_" },
+ "backbone": { exports: "Backbone", deps: [ 'jquery', 'underscore' ] },
+ "hitch": { exports: "Hitch", deps: [ 'jquery', 'underscore', 'backbone' ] }
+ },
+
+ name: "main",
+
+ out: "<%= out %>",
+
+ wrap: true
+
+})
View
23 lib/data/config.ejs
@@ -1,26 +1,15 @@
({
- baseUrl: "../public/js",
+name: "<%= name %>",
- paths: {
- "jquery": "vendor/jquery",
- "underscore": "vendor/underscore",
- "backbone": "vendor/backbone",
- "hitch": "vendor/hitch",
- "text": "vendor/text"
- },
+version: "<%= version %>",
- shim: {
- "jquery": { exports: "$" },
- "underscore": { exports: "_" },
- "backbone": { exports: "Backbone", deps: [ 'jquery', 'underscore' ] },
- "hitch": { exports: "Hitch", deps: [ 'jquery', 'underscore', 'backbone' ] }
- },
+exports: <%= exports %>,
- name: "main",
+apiUrl: "<%= apiUrl %>",
- out: "<%= out %>",
+root: "<%= root %>",
- wrap: true
+pushState: <%= pushState %>
})
View
15 lib/data/hitch.json.ejs
@@ -1,15 +0,0 @@
-{
-
- "name": "<%= name %>",
-
- "version": "<%= version %>",
-
- "exports": "<%= exports %>",
-
- "apiUrl": "<%= apiUrl %>",
-
- "root": "<%= root %>",
-
- "pushState": "<%= pushState %>"
-
-}
View
6 lib/data/index.ejs
@@ -1,10 +1,10 @@
<!DOCTYPE html>
-<html>
+<html id="<%= name %>">
<head>
<meta charset="utf8">
<title><%= name %></title>
- <script type="text/javascript" data-main="<%= root %>js/main" src="<%= root %>js/vendor/require/require.js"></script>
+ <script type="text/javascript" data-main="<%= pushState ? root : '' %>js/main" src="<%= pushState ? root : '' %>js/vendor/require/require.js"></script>
</head>
-<body id="<%= name %>">
+<body>
</body>
</html>
View
3 lib/data/main.ejs
@@ -73,8 +73,7 @@ require([
%>
this.<%= p %> = new <%= r %>({
- currentUser: this.getCurrentUser()<%
-
+ <%
var i = z = y = -1
, l = _.size(c);
View
44 lib/functions.js
@@ -84,14 +84,30 @@ function exists(name) {
return false;
}
-function parseJSONFile(file) {
- var contents, raw;
+function parseFile(file) {
+
+ var contents
+ , raw
+ , ext;
+
if (exists(file)) {
- contents = JSON.parse(fs.readFileSync(file));
- _.each(contents, function(val, key) {
- contents[key] = val === 'true' ? true : val === 'false' ? false : val;
- });
+
+ ext = _.last(file.split('.'));
+ raw = fs.readFileSync(file).toString();
+
+ try {
+ if (ext === 'json') {
+ contents = JSON.parse(raw);
+ } else if (ext === 'js') {
+ eval('contents = ' + raw);
+ } else {
+ contents = raw;
+ }
+ } catch(err) {
+ throw new Error("invalid file in '" + file + "'.");
+ }
}
+
return contents;
}
@@ -146,7 +162,7 @@ function buildTarget(target, path) {
}
path = path || process.cwd();
- data = parseJSONFile(path + '/hitch.json');
+ data = parseFile(path + '/app.js');
if (!data) {
throw new Error("invalid location. no hitch.json found in path");
@@ -198,7 +214,7 @@ function createTarget(target) {
var path = process.cwd()
, args = _.rest(arguments)
- , data = parseJSONFile(path + '/hitch.json');
+ , data = parseFile(path + '/app.js');
if (!target) {
throw new Error("invalid target");
@@ -310,8 +326,8 @@ function createApplication(path) {
appName = path.split('/').pop();
// ensure hitch.json
- if (!exists(path + '/hitch.json')) {
- createFileFromTemplate(path + '/hitch.json', 'hitch.json', {
+ if (!exists(path + '/app.js')) {
+ createFileFromTemplate(path + '/app.js', 'config', {
name: appName,
apiUrl: '/api',
version: '1.0',
@@ -322,7 +338,7 @@ function createApplication(path) {
}
// read configuration
- data = parseJSONFile(path + '/hitch.json');
+ data = parseFile(path + '/app.js');
// check configuration
if (!data || !data.name || !data.version) {
@@ -372,7 +388,7 @@ function createApplication(path) {
function deploy(version) {
- var data = parseJSONFile('hitch.json')
+ var data = parseFile('app.js')
, buildPath = process.cwd() + '/build'
, buildConfig = buildPath + '/config.js'
, deploymentPath
@@ -416,7 +432,7 @@ function deploy(version) {
});
}
- createFileFromTemplate(buildConfig, 'config', {
+ createFileFromTemplate(buildConfig, 'build', {
out: data.version + '/main-' + data.version + '.js'
}, function() {
console.log('> created build configuration');
@@ -441,7 +457,7 @@ function deploy(version) {
function showHelp() {
- var pkg = parseJSONFile(PATH_ROOT + '/package.json');
+ var pkg = parseFile(PATH_ROOT + '/package.json');
console.log('+' + _strRepeat('-', 73) + '+');
console.log('|' + _strCenter('Hitch Command Line Tools', 73) + '|');
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "backbone-hitch",
- "version": "0.1.0-alpha",
+ "version": "0.1.0",
"description": "Lightweight framework built on top of backbone",
"keywords": [
"framework",
View
35 test/tests.js
@@ -58,31 +58,6 @@
});
- test('ACL Unit Tests', function() {
-
- var acl = a.getACL()
- , usr = new Hitch.User({ _id: 1 });
-
- equal(acl._determineUserId(usr), 1);
-
- acl.setPublicReadAccess(true);
- deepEqual(acl.getPublicReadAccess(), true);
-
- acl.setPublicWriteAccess(true);
- deepEqual(acl.getPublicWriteAccess(), true);
-
- acl.setRoleReadAccess(usr.role, true);
- deepEqual(acl.getRoleReadAccess(usr.role), true);
-
- acl.setReadAccess(usr, true);
- deepEqual(acl.getReadAccess(usr), true);
-
- acl.getWriteAccess(usr, true);
- deepEqual(acl.getReadAccess(usr), true);
-
- deepEqual(acl.toJSON(), acl.permissions);
- });
-
/**
* @module: Hitch.Resource
@@ -211,16 +186,6 @@
});
-
-
- module('Hitch.AccessControlList');
- test('Hitch.AccessControlList', function() {
-
-
-
- });
-
-
}).call(this);

0 comments on commit e10e4f7

Please sign in to comment.
Something went wrong with that request. Please try again.