Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* update to v0.1.0 RC-1

  • Loading branch information...
commit e10e4f764ffa88eb82b1e5ece650d59f4360a4b5 1 parent 42c5569
Philipp Boes authored
3  .gitignore
... ... @@ -1,3 +1,4 @@
1 1 node_modules
2 2 .npmignore
3   -.idea
  3 +.idea
  4 +CHANGES.md
9 README.md
Source Rendered
... ... @@ -1,8 +1,7 @@
1   -Hitch v0.1.0-alpha
2   -=====
3   -Lightweight backbone-based single page application framework.
  1 +Hitch v0.1.0
  2 +==================
4 3
5   -***Hitch is currently in a very early alpha state.***
  4 +Lightweight backbone-based single page application framework.
6 5
7 6 * Default [Hitch.Object](#hitchobject)s for [users](#hitchuser), [credentials](#hitchcredentials) and [roles](#hitchrole)
8 7 * Powerful [Access Control Layer](#hitchacl) for controlling access to [Hitch.Router](#hitchobject)s
@@ -239,4 +238,4 @@ The enhanced Backbone.Router
239 238 A default user model representation
240 239
241 240 ### Hitch.View
242   -The enhanced Backbone.View
  241 +The enhanced Backbone.View
2  bin/hitch
@@ -6,7 +6,7 @@
6 6 *
7 7 * @author: Philipp Boes <mostgreedy@gmail.com>
8 8 * @copyright: (c) 2012 Philipp Boes
9   - * @version: 0.1.0-alpha
  9 + * @version: 0.1.0
10 10 */
11 11
12 12 var buildTarget = require('../lib/functions').buildTarget
347 hitch.js
... ... @@ -1,10 +1,10 @@
1 1 /**
2   - * Hitch.js - v0.1.0-alpha
  2 + * Hitch.js - v0.1.0
3 3 * Lightweight backbone based single page application framework
4 4 *
5 5 * @author: Philipp Boes <mostgreedy@gmail.com>
6 6 * @copyright: (c) 2012 Philipp Boes
7   - * @version: 0.1.0-alpha
  7 + * @version: 0.1.0
8 8 *
9 9 */
10 10 (function() {
@@ -24,7 +24,7 @@
24 24 extend = Backbone.Router.extend;
25 25
26 26 // keep in sync with package.json
27   - Hitch.VERSION = '0.1.0-alpha';
  27 + Hitch.VERSION = '0.1.0';
28 28
29 29 /**
30 30 * Hitch.Access Mixin
@@ -44,7 +44,7 @@
44 44 this.acl = new Hitch.ACL();
45 45 }
46 46
47   - return this.acl
  47 + return this.acl;
48 48 },
49 49
50 50 /**
@@ -62,7 +62,7 @@
62 62 };
63 63
64 64 /**
65   - * Hitch.Cookies Mixin
  65 + * Hitch.Cookies
66 66 * @type {Object}
67 67 */
68 68 Hitch.Cookies = {
@@ -294,21 +294,27 @@
294 294 _.mixin(Hitch.Helpers);
295 295
296 296 /**
297   - * Hitch.AccessControlList
  297 + * Hitch.ACL
298 298 * @constructor
299 299 */
300   - Hitch.AccessControlList = function() {
  300 + Hitch.ACL = function(obj, permissions) {
  301 +
301 302 this._publicPermissions = {};
302 303 this._routePermissions = {};
  304 +
  305 + if ($.isPlainObject(obj) && obj.public && obj.routes) {
  306 + this._publicPermissions = obj.public;
  307 + this._routePermissions = obj.routes;
  308 + }
303 309 };
304 310
305 311 /**
306   - * Hitch.AccessControlList.prototype
  312 + * Hitch.ACL.prototype
307 313 * @type {Object}
308 314 */
309   - Hitch.AccessControlList.prototype = {
  315 + Hitch.ACL.prototype = {
310 316
311   - constructor: Hitch.AccessControlList,
  317 + constructor: Hitch.ACL,
312 318
313 319 getAccess: function(route, obj) {
314 320
@@ -344,241 +350,23 @@
344 350 permissions = !!permissions;
345 351 }
346 352
347   - if (!this._routePermissions[route]) {
348   - this._routePermissions[route] = {};
349   - }
350   -
351   - this._routePermissions[route][obj.id] = permissions;
352   - }
353   - };
354   -
355   - /**
356   - * Hitch.ACL
357   - * @param permissions
358   - * @constructor
359   - */
360   - Hitch.ACL = function(permissions) {
361   -
362   - this.permissions = {};
363   -
364   - if (permissions instanceof Hitch.Object || permissions instanceof Hitch.Resource) {
365   -
366   - this.setReadAccess(permissions, true);
367   - this.setWriteAccess(permissions, true);
368   -
369   - } else if (_.isObject(permissions)) {
370   -
371   - _.each(permissions, function(accessList, userId) {
  353 + if (route === '*') {
372 354
373   - this.permissions[userId] = {};
  355 + this._publicPermissions[route] = permissions;
374 356
375   - _.each(accessList, function(allowed, permission) {
376   - this.permissions[userId][permission] = allowed;
377   - }, this);
378   -
379   - }, this);
380   -
381   - }
382   - };
383   -
384   - // Public access role
385   - Hitch.ACL.PUBLIC = 'PUBLIC';
386   -
387   - /**
388   - * Hitch.ACL.prototype
389   - * @type {Object}
390   - */
391   - Hitch.ACL.prototype = {
392   -
393   - constructor: Hitch.ACL,
394   -
395   - /**
396   - * Returns the user id
397   - * @param userId
398   - * @return {*}
399   - * @private
400   - */
401   - _determineUserId: function(userId) {
402   - if (userId instanceof Hitch.User) {
403   - return userId.id;
404   - } else if (userId instanceof Hitch.Role) {
405   - return 'role:' + userId.getName();
406   - } else if (userId instanceof Hitch.Resource) {
407   - return 'resource:' + userId.name;
408   - } else if (_.isObject(userId)) {
409   - return userId.toString();
410 357 } else {
411   - return userId;
412   - }
413   - },
414   -
415   - /**
416   - * Returns access permissions for the given access type and user id
417   - * @param accessType
418   - * @param userId
419   - * @return {*}
420   - * @private
421   - */
422   - _getAccess: function(accessType, userId) {
423   -
424   - var permissions;
425   -
426   - userId = this._determineUserId(userId);
427   - permissions = this.permissions[userId];
428   -
429   - if (!permissions) {
430   - return false;
431   - }
432   -
433   - if (!permissions[accessType]) {
434   - return false;
435   - };
436   -
437   - return permissions[accessType];
438   - },
439   -
440   - /**
441   - * Sets the access rights for the given access type and user id
442   - * @param accessType
443   - * @param userId
444   - * @param allowed
445   - * @private
446   - */
447   - _setAccess: function(accessType, userId, allowed) {
448   -
449   - var permissions;
450 358
451   - userId = this._determineUserId(userId);
452   - permissions = this.permissions[userId];
453   -
454   - if (!permissions) {
455   - if (!allowed) return;
456   - permissions = {};
457   - this.permissions[userId] = permissions;
458   - }
459   -
460   - if (allowed) {
461   - this.permissions[userId][accessType] = true;
462   - } else {
463   - delete permissions[accessType];
464   - if (_.isEmpty(permissions)) {
465   - delete permissions[userId];
  359 + if (!this._routePermissions[route]) {
  360 + this._routePermissions[route] = {};
466 361 }
467   - }
468   - },
469   -
470   - /**
471   - * Returns the public read access permission
472   - * @return {*}
473   - */
474   - getPublicReadAccess: function() {
475   - return this._getAccess('read', Hitch.ACL.PUBLIC);
476   - },
477   -
478   - /**
479   - * Sets the public read access permission
480   - * @param allowed
481   - */
482   - setPublicReadAccess: function(allowed) {
483   - this._setAccess('read', Hitch.ACL.PUBLIC, allowed);
484   - },
485   -
486   - /**
487   - * Returns role read permissions
488   - * @param role
489   - * @return {*}
490   - */
491   - getRoleReadAccess: function(role) {
492   - return this._getAccess('read', role);
493   - },
494 362
495   - /**
496   - * Sets read access permissions for the given role
497   - * @param role
498   - * @param allowed
499   - */
500   - setRoleReadAccess: function(role, allowed) {
501   - this._setAccess('read', role, allowed);
502   - },
503   -
504   - /**
505   - * Returns the read access permission for a given user id
506   - * @param userId
507   - * @return {*}
508   - */
509   - getReadAccess: function(userId) {
510   - return this._getAccess('read', userId);
511   - },
512   -
513   - /**
514   - * Sets the read access permission for a given user id
515   - * @param userId
516   - * @param allowed
517   - */
518   - setReadAccess: function(userId, allowed) {
519   - this._setAccess('read', userId, allowed);
520   - },
521   -
522   - /**
523   - * Returns the write access permission for a given user id
524   - * @param userId
525   - * @return {*}
526   - */
527   - getWriteAccess: function(userId) {
528   - return this._getAccess('write', userId);
529   - },
530   -
531   - /**
532   - * Sets the write access permission for a given user id
533   - * @param userId
534   - * @param allowed
535   - */
536   - setWriteAccess: function(userId, allowed) {
537   - this._setAccess('write', userId, allowed);
538   - },
539   -
540   - /**
541   - * Returns the public write access permission
542   - * @return {*}
543   - */
544   - getPublicWriteAccess: function() {
545   - return this._getAccess('write', Hitch.ACL.PUBLIC);
546   - },
547   -
548   - /**
549   - * Sets the public write access permission
550   - * @param allowed
551   - */
552   - setPublicWriteAccess: function(allowed) {
553   - this._setAccess('write', Hitch.ACL.PUBLIC, allowed);
554   - },
555   -
556   - /**
557   - * Returns the write access permission for a given role
558   - * @param role
559   - * @return {*}
560   - */
561   - getRoleWriteAccess: function(role) {
562   - return this._getAccess('write', Hitch.ACL.PUBLIC);
563   - },
564   -
565   - /**
566   - * Sets the write access permission for a given role
567   - * @param role
568   - * @param allowed
569   - */
570   - setRoleWriteAccess: function(role, allowed) {
571   - this._setAccess('write', role, allowed);
  363 + this._routePermissions[route][obj.id] = permissions;
  364 + }
572 365 },
573 366
574   - /**
575   - * Returns a JSON representation of this ACL's permissions
576   - * @return {*}
577   - */
578 367 toJSON: function() {
579   - return _.clone(this.permissions);
  368 + return { public: this._publicPermissions, routes: this._routePermissions };
580 369 }
581   -
582 370 };
583 371
584 372 /**
@@ -763,6 +551,20 @@
763 551 }
764 552
765 553 return returnVal(model.get(attr), value, operator);
  554 + },
  555 +
  556 + load: function(options) {
  557 +
  558 + var success;
  559 +
  560 + options = options || {};
  561 + success = options.success;
  562 +
  563 + options.success = _.bind(function() {
  564 + this.trigger('load', this);
  565 + }, this);
  566 +
  567 + return this.fetch(options);
766 568 }
767 569
768 570 }));
@@ -1065,19 +867,6 @@
1065 867 // route filters
1066 868 _filters: {},
1067 869
1068   - getCurrentUser: function(currentUser) {
1069   -
1070   - if (!this.currentUser) {
1071   - if (currentUser instanceof Hitch.User) {
1072   - this.currentUser = currentUser;
1073   - } else {
1074   - this.currentUser = new Hitch.User();
1075   - }
1076   - }
1077   -
1078   - return this.currentUser;
1079   - },
1080   -
1081 870 /**
1082 871 * Adds an after filter
1083 872 * @param route
@@ -1119,11 +908,8 @@
1119 908
1120 909 var args = this._extractParameters(route, fragment)
1121 910 , acl = this.getACL()
1122   - , canAccessRoute = acl.getAccess(name);
1123   -
1124   - console.log(canAccessRoute, acl);
1125   - if (!canAccessRoute) return;
1126 911
  912 + if (!acl.getAccess(name)) return;
1127 913 if (this._applyFilters('before', fragment, args)) {
1128 914
1129 915 callback && callback.apply(this, args);
@@ -1391,56 +1177,6 @@
1391 1177 },
1392 1178
1393 1179 /**
1394   - * Returns the public interface for this application
1395   - * @return {*}
1396   - */
1397   - getPublicInterface: function() {
1398   - var publicInterfaceMethodNames = _.filter(_.keys(this), function(key) { return key.charAt(0) !== '_'; })
1399   - return _.pick(this, publicInterfaceMethodNames);
1400   - },
1401   -
1402   - /**
1403   - * Returns the current session user
1404   - * @return {Hitch.User}
1405   - */
1406   - getCurrentUser: function() {
1407   -
1408   - if (!this.currentUser) {
1409   -
1410   - var cookie = Hitch.Cookies.get('hitch-user')
1411   - , self = this
1412   - , user;
1413   -
1414   - if (cookie) {
1415   -
1416   - user = new Hitch.User(cookie);
1417   - user.fetch({
1418   -
1419   - success: function(data) {
1420   - user.set(data);
1421   - self.currentUser = user;
1422   - },
1423   -
1424   - error: function() {
1425   - Hitch.Cookies.clear('hitch-user');
1426   - user = new Hitch.User({ role: { name: 'visitor' } });
1427   - Hitch.Cookies.set('hitch-user', user.id);
1428   - }
1429   -
1430   - });
1431   -
1432   - } else {
1433   - user = new Hitch.User({ role: { name: 'visitor' } });
1434   - Hitch.Cookies.set('hitch-user', user.id);
1435   - }
1436   -
1437   - this.currentUser = user;
1438   - }
1439   -
1440   - return this.currentUser;
1441   - },
1442   -
1443   - /**
1444 1180 * Appends an asset to the document head
1445 1181 * @param type
1446 1182 * @param source
@@ -1488,10 +1224,9 @@
1488 1224 resource.url = [ this.apiUrl, resource.name ].join('/');
1489 1225 }
1490 1226
1491   - resource.fetch({
  1227 + resource.load({
1492 1228 success: _.bind(function() {
1493 1229 this.resources[resource.name] = resource;
1494   - resource.trigger('load', resource);
1495 1230 if (++loaded === length) {
1496 1231 this.trigger('ready', this.resources);
1497 1232 }
@@ -1522,7 +1257,7 @@
1522 1257
1523 1258 if (this.exports) {
1524 1259 var globalName = _.isString(this.exports) ? this.exports : this.name;
1525   - root[globalName] = this.getPublicInterface();
  1260 + root[globalName] = this;
1526 1261 }
1527 1262 },
1528 1263
@@ -1577,4 +1312,4 @@
1577 1312 }
1578 1313 };
1579 1314
1580   -}).call(this);
  1315 +}).call(this);
2  lib/console_utils.js
@@ -54,4 +54,4 @@ ConsoleUtils.Command.prototype.accepts = function(args) {
54 54 return true;
55 55 };
56 56
57   -exports = module.exports = ConsoleUtils;
  57 +exports = module.exports = ConsoleUtils;
26 lib/data/build.ejs
... ... @@ -0,0 +1,26 @@
  1 +({
  2 +
  3 + baseUrl: "../public/js",
  4 +
  5 + paths: {
  6 + "jquery": "vendor/jquery",
  7 + "underscore": "vendor/underscore",
  8 + "backbone": "vendor/backbone",
  9 + "hitch": "vendor/hitch",
  10 + "text": "vendor/text"
  11 + },
  12 +
  13 + shim: {
  14 + "jquery": { exports: "$" },
  15 + "underscore": { exports: "_" },
  16 + "backbone": { exports: "Backbone", deps: [ 'jquery', 'underscore' ] },
  17 + "hitch": { exports: "Hitch", deps: [ 'jquery', 'underscore', 'backbone' ] }
  18 + },
  19 +
  20 + name: "main",
  21 +
  22 + out: "<%= out %>",
  23 +
  24 + wrap: true
  25 +
  26 +})
23 lib/data/config.ejs
... ... @@ -1,26 +1,15 @@
1 1 ({
2 2
3   - baseUrl: "../public/js",
  3 +name: "<%= name %>",
4 4
5   - paths: {
6   - "jquery": "vendor/jquery",
7   - "underscore": "vendor/underscore",
8   - "backbone": "vendor/backbone",
9   - "hitch": "vendor/hitch",
10   - "text": "vendor/text"
11   - },
  5 +version: "<%= version %>",
12 6
13   - shim: {
14   - "jquery": { exports: "$" },
15   - "underscore": { exports: "_" },
16   - "backbone": { exports: "Backbone", deps: [ 'jquery', 'underscore' ] },
17   - "hitch": { exports: "Hitch", deps: [ 'jquery', 'underscore', 'backbone' ] }
18   - },
  7 +exports: <%= exports %>,
19 8
20   - name: "main",
  9 +apiUrl: "<%= apiUrl %>",
21 10
22   - out: "<%= out %>",
  11 +root: "<%= root %>",
23 12
24   - wrap: true
  13 +pushState: <%= pushState %>
25 14
26 15 })
15 lib/data/hitch.json.ejs
... ... @@ -1,15 +0,0 @@
1   -{
2   -
3   - "name": "<%= name %>",
4   -
5   - "version": "<%= version %>",
6   -
7   - "exports": "<%= exports %>",
8   -
9   - "apiUrl": "<%= apiUrl %>",
10   -
11   - "root": "<%= root %>",
12   -
13   - "pushState": "<%= pushState %>"
14   -
15   -}
6 lib/data/index.ejs
... ... @@ -1,10 +1,10 @@
1 1 <!DOCTYPE html>
2   -<html>
  2 +<html id="<%= name %>">
3 3 <head>
4 4 <meta charset="utf8">
5 5 <title><%= name %></title>
6   - <script type="text/javascript" data-main="<%= root %>js/main" src="<%= root %>js/vendor/require/require.js"></script>
  6 + <script type="text/javascript" data-main="<%= pushState ? root : '' %>js/main" src="<%= pushState ? root : '' %>js/vendor/require/require.js"></script>
7 7 </head>
8   -<body id="<%= name %>">
  8 +<body>
9 9 </body>
10 10 </html>
3  lib/data/main.ejs
@@ -73,8 +73,7 @@ require([
73 73
74 74 %>
75 75 this.<%= p %> = new <%= r %>({
76   - currentUser: this.getCurrentUser()<%
77   -
  76 + <%
78 77 var i = z = y = -1
79 78 , l = _.size(c);
80 79
44 lib/functions.js
@@ -84,14 +84,30 @@ function exists(name) {
84 84 return false;
85 85 }
86 86
87   -function parseJSONFile(file) {
88   - var contents, raw;
  87 +function parseFile(file) {
  88 +
  89 + var contents
  90 + , raw
  91 + , ext;
  92 +
89 93 if (exists(file)) {
90   - contents = JSON.parse(fs.readFileSync(file));
91   - _.each(contents, function(val, key) {
92   - contents[key] = val === 'true' ? true : val === 'false' ? false : val;
93   - });
  94 +
  95 + ext = _.last(file.split('.'));
  96 + raw = fs.readFileSync(file).toString();
  97 +
  98 + try {
  99 + if (ext === 'json') {
  100 + contents = JSON.parse(raw);
  101 + } else if (ext === 'js') {
  102 + eval('contents = ' + raw);
  103 + } else {
  104 + contents = raw;
  105 + }
  106 + } catch(err) {
  107 + throw new Error("invalid file in '" + file + "'.");
  108 + }
94 109 }
  110 +
95 111 return contents;
96 112 }
97 113
@@ -146,7 +162,7 @@ function buildTarget(target, path) {
146 162 }
147 163
148 164 path = path || process.cwd();
149   - data = parseJSONFile(path + '/hitch.json');
  165 + data = parseFile(path + '/app.js');
150 166
151 167 if (!data) {
152 168 throw new Error("invalid location. no hitch.json found in path");
@@ -198,7 +214,7 @@ function createTarget(target) {
198 214
199 215 var path = process.cwd()
200 216 , args = _.rest(arguments)
201   - , data = parseJSONFile(path + '/hitch.json');
  217 + , data = parseFile(path + '/app.js');
202 218
203 219 if (!target) {
204 220 throw new Error("invalid target");
@@ -310,8 +326,8 @@ function createApplication(path) {
310 326 appName = path.split('/').pop();
311 327
312 328 // ensure hitch.json
313   - if (!exists(path + '/hitch.json')) {
314   - createFileFromTemplate(path + '/hitch.json', 'hitch.json', {
  329 + if (!exists(path + '/app.js')) {
  330 + createFileFromTemplate(path + '/app.js', 'config', {
315 331 name: appName,
316 332 apiUrl: '/api',
317 333 version: '1.0',
@@ -322,7 +338,7 @@ function createApplication(path) {
322 338 }
323 339
324 340 // read configuration
325   - data = parseJSONFile(path + '/hitch.json');
  341 + data = parseFile(path + '/app.js');
326 342
327 343 // check configuration
328 344 if (!data || !data.name || !data.version) {
@@ -372,7 +388,7 @@ function createApplication(path) {
372 388
373 389 function deploy(version) {
374 390
375   - var data = parseJSONFile('hitch.json')
  391 + var data = parseFile('app.js')
376 392 , buildPath = process.cwd() + '/build'
377 393 , buildConfig = buildPath + '/config.js'
378 394 , deploymentPath
@@ -416,7 +432,7 @@ function deploy(version) {
416 432 });
417 433 }
418 434
419   - createFileFromTemplate(buildConfig, 'config', {
  435 + createFileFromTemplate(buildConfig, 'build', {
420 436 out: data.version + '/main-' + data.version + '.js'
421 437 }, function() {
422 438 console.log('> created build configuration');
@@ -441,7 +457,7 @@ function deploy(version) {
441 457
442 458 function showHelp() {
443 459
444   - var pkg = parseJSONFile(PATH_ROOT + '/package.json');
  460 + var pkg = parseFile(PATH_ROOT + '/package.json');
445 461
446 462 console.log('+' + _strRepeat('-', 73) + '+');
447 463 console.log('|' + _strCenter('Hitch Command Line Tools', 73) + '|');
2  package.json
... ... @@ -1,6 +1,6 @@
1 1 {
2 2 "name": "backbone-hitch",
3   - "version": "0.1.0-alpha",
  3 + "version": "0.1.0",
4 4 "description": "Lightweight framework built on top of backbone",
5 5 "keywords": [
6 6 "framework",
35 test/tests.js
@@ -58,31 +58,6 @@
58 58
59 59 });
60 60
61   - test('ACL Unit Tests', function() {
62   -
63   - var acl = a.getACL()
64   - , usr = new Hitch.User({ _id: 1 });
65   -
66   - equal(acl._determineUserId(usr), 1);
67   -
68   - acl.setPublicReadAccess(true);
69   - deepEqual(acl.getPublicReadAccess(), true);
70   -
71   - acl.setPublicWriteAccess(true);
72   - deepEqual(acl.getPublicWriteAccess(), true);
73   -
74   - acl.setRoleReadAccess(usr.role, true);
75   - deepEqual(acl.getRoleReadAccess(usr.role), true);
76   -
77   - acl.setReadAccess(usr, true);
78   - deepEqual(acl.getReadAccess(usr), true);
79   -
80   - acl.getWriteAccess(usr, true);
81   - deepEqual(acl.getReadAccess(usr), true);
82   -
83   - deepEqual(acl.toJSON(), acl.permissions);
84   - });
85   -
86 61
87 62 /**
88 63 * @module: Hitch.Resource
@@ -211,16 +186,6 @@
211 186 });
212 187
213 188
214   -
215   -
216   - module('Hitch.AccessControlList');
217   - test('Hitch.AccessControlList', function() {
218   -
219   -
220   -
221   - });
222   -
223   -
224 189 }).call(this);
225 190
226 191

0 comments on commit e10e4f7

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