Permalink
Browse files

Merge pull request #1 from yuxel/seperation

Seperation
  • Loading branch information...
dashersw committed May 24, 2011
2 parents f11565f + ff741bc commit e54de1c52390c998c62772b0f804330fe65aa086
Showing with 94 additions and 61 deletions.
  1. +7 −2 js/base/base.js
  2. +7 −5 js/base/uri/{request.js → Request.js}
  3. +77 −50 js/base/uri/{Router.js → router.js}
  4. +3 −4 js/deps.js
View
@@ -12,16 +12,21 @@
goog.require('dashMVC.Controller');
goog.require('dashMVC.Model');
goog.require('dashMVC.View');
-goog.require('dashMVC.uriRouter');
+goog.require('dashMVC.uri.router');
goog.require('goog.History');
goog.provide('dashMVC');
dashMVC.history = new goog.History(false);
+/**
+ * TODO: this path should be changed, or can be set from a parameter from server
+ */
+dashMVC.uri.router.setBasePath('/github/dashMVC/');
+
dashMVC.history.callback = function(e) {
- dashMVC.uriRouter.init();
+ dashMVC.uri.router.init();
}
goog.events.listen(dashMVC.history, goog.history.EventType.NAVIGATE, dashMVC.history.callback);
@@ -3,7 +3,7 @@
//
// @author Armagan Amcalar <armagan@tart.com.tr>
-goog.provide('dashMVC.Request');
+goog.provide('dashMVC.uri.Request');
goog.require('goog.Uri');
@@ -12,19 +12,21 @@ goog.require('goog.Uri');
* Represents a request made to the application, storing its controller, action and parameters.
* @constructor
* @param uri
+ * @param {string} basePath uri base path.
*/
-dashMVC.Request = function(uri) {
+dashMVC.uri.Request = function(uri) {
+ basePath = dashMVC.uri.router.getBasePath();
var uri = new goog.Uri(uri);
this.path = uri.getPath();
this.fragment = uri.getFragment();
- if (this.path == "/" && this.fragment.substr(0, 2) == '!/') {
+ if (this.path == basePath && this.fragment.substr(0, 2) == '!/') {
this.path = this.fragment.substr(2);
}
- this.fragments = this.path.split("/");
+ this.fragments = this.path.split('/');
this.fragments = goog.array.filter(this.fragments, function(el, i, arr) {
- return (el != '')
+ return (el != '');
});
};
@@ -3,41 +3,68 @@
//
// @author Armagan Amcalar <armagan@tart.com.tr>
-goog.provide('dashMVC.uriRouter');
-goog.require('dashMVC.Request');
+goog.provide('dashMVC.uri.router');
+goog.require('dashMVC.uri.Request');
goog.require('goog.array');
goog.require('goog.object');
+/**
+ * Varible to hold uri base path
+ *
+ * @private
+ */
+dashMVC.uri.router.basePath_ = '/';
+
/**
- * UriRouter singleton. It is responsible for routing the incoming request to appropriate controller and actions
+ * uri.router singleton. It is responsible for routing the incoming request to appropriate controller and actions
* with appropriate parameters.
- * The application routes and aliases are added to the uriRouter and every time the URI
+ * The application routes and aliases are added to the uri.router and every time the URI
* changes, it routes the request to the appropriate controller/action.
* @param {string} uri The URI to parse.
*/
-dashMVC.uriRouter.init = function(uri) {
- dashMVC.uriRouter.request = new dashMVC.Request(uri || window.location);
+dashMVC.uri.router.init = function(uri) {
+ var basePath = dashMVC.uri.router.getBasePath();
+ dashMVC.uri.router.request = new dashMVC.uri.Request(uri || window.location);
- dashMVC.uriRouter.resolveAliases_(dashMVC.uriRouter.request);
- dashMVC.uriRouter.process_(dashMVC.uriRouter.request);
+ dashMVC.uri.router.resolveAliases_(dashMVC.uri.router.request);
+ dashMVC.uri.router.process_(dashMVC.uri.router.request);
- console.log(dashMVC.uriRouter.getController(), dashMVC.uriRouter.getAction(), dashMVC.uriRouter.getParams());
-}
+ console.log(dashMVC.uri.router.getController(), dashMVC.uri.router.getAction(), dashMVC.uri.router.getParams());
+};
+
+
+/**
+ * Set base path
+ *
+ * @param {string} path uri base path.
+ */
+dashMVC.uri.router.setBasePath = function(path) {
+ dashMVC.uri.router.basePath_ = path || '/';
+};
+
+/**
+ * Return uri base path
+ *
+ * @return {string} uri base path.
+ */
+dashMVC.uri.router.getBasePath = function() {
+ return dashMVC.uri.router.basePath_;
+};
/**
* Resolves aliases to real URI schemes.
* If the request matches any alias, this function resolves it to the original scheme.
* @private
- * @param {dashMVC.request} request Request to look for an alias match.
+ * @param {dashMVC.uri.Request} request Request to look for an alias match.
*/
-dashMVC.uriRouter.resolveAliases_ = function(request) {
+dashMVC.uri.router.resolveAliases_ = function(request) {
var response, scheme;
- goog.array.some(dashMVC.uriRouter.aliases, function(alias, i, arr) {
+ goog.array.some(dashMVC.uri.router.aliases, function(alias, i, arr) {
if (response = request.path.match(alias.format)) {
- scheme = dashMVC.uriRouter.getScheme(alias.schemeName);
+ scheme = dashMVC.uri.router.getScheme(alias.schemeName);
var fragments = [alias.schemeName, alias.schemeAction];
for (var i = 0; i < response.length - 1; i++) {
fragments.push(alias.params[i], response[i + 1]);
@@ -53,36 +80,36 @@ dashMVC.uriRouter.resolveAliases_ = function(request) {
/**
* This function sets the current scheme and the related controllers, actions and parameters.
* @private
- * @param {dashMVC.Request} request Request to be processed.
+ * @param {dashMVC.uri.Request} request Request to be processed.
*/
-dashMVC.uriRouter.process_ = function(request) {
+dashMVC.uri.router.process_ = function(request) {
var fragments = goog.array.clone(request.fragments);
- dashMVC.uriRouter.currentScheme = dashMVC.uriRouter.schemes[fragments[0]] || dashMVC.uriRouter.schemes.def;
- dashMVC.uriRouter.setController_(fragments.shift());
- dashMVC.uriRouter.setAction_(fragments.shift());
- dashMVC.uriRouter.setParams_(fragments);
+ dashMVC.uri.router.currentScheme = dashMVC.uri.router.schemes[fragments[0]] || dashMVC.uri.router.schemes.def;
+ dashMVC.uri.router.setController_(fragments.shift());
+ dashMVC.uri.router.setAction_(fragments.shift());
+ dashMVC.uri.router.setParams_(fragments);
};
/**
* Sets the current controller required by the request. If there are no such controllers, default controller is set.
* @private
* @param {string} controller Dummy parameter for controller name (This function already takes the controller name
* from the current scheme, but in order for fragments.shift() to work in
- * @link{dashMVC.uriRouter.process_}, this function should receive a parameter.
+ * @link{dashMVC.uri.router.process_}, this function should receive a parameter.
*/
-dashMVC.uriRouter.setController_ = function(controller) {
- dashMVC.uriRouter.controller_ = dashMVC.uriRouter.currentScheme.controller;
+dashMVC.uri.router.setController_ = function(controller) {
+ dashMVC.uri.router.controller_ = dashMVC.uri.router.currentScheme.controller;
};
/**
* Sets the current action required by the request. If there are no such actions, default action is set.
* @private
* @param {string} action Action present on the scheme.
*/
-dashMVC.uriRouter.setAction_ = function(action) {
- dashMVC.uriRouter.action_ = dashMVC.uriRouter.currentScheme.actions[action] ||
- dashMVC.uriRouter.schemes.def.actions.def;
+dashMVC.uri.router.setAction_ = function(action) {
+ dashMVC.uri.router.action_ = dashMVC.uri.router.currentScheme.actions[action] ||
+ dashMVC.uri.router.schemes.def.actions.def;
};
/**
@@ -91,61 +118,61 @@ dashMVC.uriRouter.setAction_ = function(action) {
* @private
* @param {Array.<string>} paramsArray Array of parameters.
*/
-dashMVC.uriRouter.setParams_ = function(paramsArray) {
+dashMVC.uri.router.setParams_ = function(paramsArray) {
var params, paramsArray = paramsArray || [];
- if (dashMVC.uriRouter.getAction() == dashMVC.uriRouter.schemes.def.actions.def) {
- dashMVC.uriRouter.params_ = {};
+ if (dashMVC.uri.router.getAction() == dashMVC.uri.router.schemes.def.actions.def) {
+ dashMVC.uri.router.params_ = {};
return;
}
if (paramsArray.length % 2 == 1)
paramsArray.push('');
params = goog.object.create(paramsArray);
- dashMVC.uriRouter.params_ = params;
+ dashMVC.uri.router.params_ = params;
};
/**
* Returns the active controller.
*/
-dashMVC.uriRouter.getController = function() {
- return dashMVC.uriRouter.controller_;
+dashMVC.uri.router.getController = function() {
+ return dashMVC.uri.router.controller_;
};
/**
* Returns the active action.
*/
-dashMVC.uriRouter.getAction = function() {
- return dashMVC.uriRouter.action_;
+dashMVC.uri.router.getAction = function() {
+ return dashMVC.uri.router.action_;
};
/**
* Returns the active parameters.
*/
-dashMVC.uriRouter.getParams = function() {
- return dashMVC.uriRouter.params_;
+dashMVC.uri.router.getParams = function() {
+ return dashMVC.uri.router.params_;
};
/**
* Adds a URI scheme to the router. This scheme will be tried upon each request.
* @param {Object} scheme Scheme to be added.
*/
-dashMVC.uriRouter.addScheme = function(scheme) {
- dashMVC.uriRouter.schemes[scheme.name] = scheme;
+dashMVC.uri.router.addScheme = function(scheme) {
+ dashMVC.uri.router.schemes[scheme.name] = scheme;
};
/**
* Returns a given URI scheme.
* @param {string} name Scheme name.
*/
-dashMVC.uriRouter.getScheme = function(name) {
- return dashMVC.uriRouter.schemes[name];
+dashMVC.uri.router.getScheme = function(name) {
+ return dashMVC.uri.router.schemes[name];
};
@@ -154,7 +181,7 @@ dashMVC.uriRouter.getScheme = function(name) {
* that scheme.
* @param {Object} alias Alias to be added.
*/
-dashMVC.uriRouter.addAlias = function(alias) {
+dashMVC.uri.router.addAlias = function(alias) {
var format = '^' + alias.format + '\/*$',
fields = format.match(/(:\w+)/g);
@@ -168,15 +195,15 @@ dashMVC.uriRouter.addAlias = function(alias) {
format = format.replace(/\//g, '\\/');
alias.format = new RegExp(format);
- dashMVC.uriRouter.aliases.push(alias);
+ dashMVC.uri.router.aliases.push(alias);
};
var defaultController = 'default c', defaultAction = 'default a';
var controller1 = 'resolved c1', action1 = 'resolved a1', action2 = 'resolved a2',
controller2 = 'resolved c2', action3 = 'resolved a3', action4 = 'resolved a4';
-dashMVC.uriRouter.aliases = [];
-dashMVC.uriRouter.schemes = {
+dashMVC.uri.router.aliases = [];
+dashMVC.uri.router.schemes = {
def: {
controller: defaultController,
actions: {
@@ -185,7 +212,7 @@ dashMVC.uriRouter.schemes = {
}
};
-dashMVC.uriRouter.addScheme({
+dashMVC.uri.router.addScheme({
name: 'controller1',
controller: controller1,
actions: {
@@ -194,7 +221,7 @@ dashMVC.uriRouter.addScheme({
}
});
-dashMVC.uriRouter.addScheme({
+dashMVC.uri.router.addScheme({
name: 'controller2',
controller: controller2,
actions: {
@@ -203,23 +230,23 @@ dashMVC.uriRouter.addScheme({
}
});
-dashMVC.uriRouter.addAlias({
+dashMVC.uri.router.addAlias({
name: 'ahmet',
format: 'ahmet/:param1/:param2',
schemeName: 'controller1',
schemeAction: 'action2'
});
-dashMVC.uriRouter.addAlias({
+dashMVC.uri.router.addAlias({
name: 'ahmet2',
format: 'ahmet/:param21',
schemeName: 'controller2',
schemeAction: 'action4'
});
-dashMVC.uriRouter.addAlias({
+dashMVC.uri.router.addAlias({
name: 'ahmet3',
format: 'ahmet',
schemeName: 'controller1',
schemeAction: 'action1'
-});
+});
View
@@ -13,7 +13,6 @@ goog.addDependency('../../base/ISubject.js', ['dashMVC.ISubject'], []);
goog.addDependency('../../base/Model.js', ['dashMVC.Model'], ['dashMVC.Subject']);
goog.addDependency('../../base/Subject.js', ['dashMVC.Subject'], ['dashMVC.ISubject', 'goog.array']);
goog.addDependency('../../base/View.js', ['dashMVC.View'], ['dashMVC.Subject']);
-goog.addDependency('../../base/base.js', ['dashMVC'], ['dashMVC.Controller', 'dashMVC.Model', 'dashMVC.View', 'dashMVC.uriRouter', 'goog.History']);
-goog.addDependency('../../base/uri/Request.js', ['dashMVC.Request'], ['goog.Uri']);
-goog.addDependency('../../base/uri/Router.js', ['dashMVC.uriRouter'], ['dashMVC.Request', 'goog.array', 'goog.object']);
-goog.addDependency('../../base/uri/Scheme.js', ['dashMVC.Scheme'], []);
+goog.addDependency('../../base/base.js', ['dashMVC'], ['dashMVC.Controller', 'dashMVC.Model', 'dashMVC.View', 'dashMVC.uri.router', 'goog.History']);
+goog.addDependency('../../base/uri/Request.js', ['dashMVC.uri.Request'], ['goog.Uri']);
+goog.addDependency('../../base/uri/router.js', ['dashMVC.uri.router'], ['dashMVC.uri.Request', 'goog.array', 'goog.object']);

0 comments on commit e54de1c

Please sign in to comment.