Created
April 29, 2013 19:11
-
-
Save tracker1/5483940 to your computer and use it in GitHub Desktop.
CSHTML Layout to get model/view/controller names into the document...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@functions{ | |
string currentController = null; | |
string currentAction = null; | |
string currentModel = null; | |
private void InitVars() { | |
ViewBag.Phone = phone; | |
currentController = ViewContext.RouteData.Values["Controller"].ToString().ToLower(); | |
currentAction = ViewContext.RouteData.Values["Action"].ToString().ToLower(); | |
currentModel = (Model == null) ? null : Model.GetType().Name; | |
// home.index returns the ~/Views/Public/VIEW.cshtml | |
if (currentController == "home" && currentAction == "index") | |
{ | |
currentController = "public"; | |
currentAction = "default"; | |
} | |
// ~/public/:VIEW - maps to public.viewpage(VIEW) which returns ~/Views/Public/VIEW.cshtml | |
if (currentController == "public" && currentAction == "viewpage") | |
{ | |
currentAction = ViewBag.ViewName; | |
} | |
} | |
}@{ InitVars(); }<!DOCTYPE html> | |
<html | |
id="@currentController-@currentAction" | |
data-model="@currentModel" | |
data-controller="@currentController" | |
data-action="@currentAction" | |
class=" | |
no-js | |
controller-@currentController | |
page-@currentController-@currentAction | |
@(User.IsInRole("Administrator") ? "admin" : "") | |
@(Request.IsAuthenticated ? "user" : "guest")" lang="en" | |
> | |
... | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Script Name: Javascript Namespace Script | |
Author: Michael J. Ryan (http://tracker1.info) | |
Public Domain | |
This library is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
*/ | |
//console.debug("begin namespace.js"); | |
(function(root){ | |
//regular expression to limit formatting of namespaces | |
var nsre = /^([\$\_a-z][\$\_a-z\d]*\.?)+$/i | |
//define returned function | |
this.namespace = function(ns) { | |
var args = Array.prototype.slice.call(arguments); | |
var ret = []; | |
while (args.length) { | |
ns = genNS(args.shift()); | |
if (ns) ret.push(ns); | |
} | |
if (ret.length == 0) return; //undefined, no valid input | |
if (arguments.length == 1) return ret[0]; //only a single input, return that namespace | |
return ret; //used overload for multiple namespaces, return the array/list | |
} | |
//private static method to generate a single namespace | |
function genNS(ns) { | |
if (!ns.match(nsre)) return; | |
ns = ns.split('.'); | |
var base = root; | |
for (var i=0; i<ns.length; i++) { | |
base[ns[i]] = base[ns[i]] || {}; | |
base = base[ns[i]]; | |
} | |
return base; //return resulting namespace object | |
} | |
}(this)); | |
//console.debug("end namespace.js"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Site Initializer Handlers - uses jQuery's $ | |
*/ | |
(function(){ | |
var init = namespace('site.init'); | |
var doc = document.getElementsByTagName("html")[0]; | |
var page = getDocAttribute("id"); | |
var model = getDocAttribute("data-model"); | |
var controller = getDocAttribute("data-controller"); | |
var action = getDocAttribute("data-action"); | |
/* | |
console.dir({ | |
doc:doc | |
,page:page | |
,model:model | |
,controller:controller | |
,action:action | |
}); | |
*/ | |
var page_inits = []; | |
var model_inits = []; | |
var controller_inits = []; | |
var action_inits = []; | |
init.page = handlePageLoadRequest; | |
init.model = handleModelLoadRequest; | |
init.controller = handleControllerLoadRequest; | |
init.action = handleControllerLoadRequest; | |
$(initialize); | |
function getDocAttribute(name) { | |
return (doc && doc.attributes[name] && doc.attributes[name].value) || false; | |
} | |
function lcv(value) { | |
if (typeof value === "string") return $.trim(value).toLowerCase(); | |
return value; | |
} | |
function initialize() { | |
var cb; | |
var list = [page_inits, model_inits, controller_inits, action_inits]; | |
for (var i=0; i<list.length; i++) { | |
for (var j=0; j<list[i].length; j++) { | |
cb = list[i][j]; | |
if (typeof cb == "function") cb(); | |
} | |
} | |
} | |
function handlePageLoadRequest(pageId, callback) { | |
if (lcv(pageId) === lcv(page)) page_inits.push(callback); | |
} | |
function handleModelLoadRequest(modelName, callback) { | |
if (lcv(modelName) == lcv(model)) model_inits.push(callback); | |
} | |
function handleControllerLoadRequest(controllerName, actionName, callback) { | |
if (typeof actionName == "function") { | |
callback = actionName; | |
actionName = null; | |
} | |
if (lcv(controllerName) !== lcv(controller)) return; //doesn't match controller | |
if (actionName === null) controller_inits.push(callback); | |
if (lcv(actionName) === lcv(action)) action_inits.push(callback); | |
} | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
site.init.action("SomeController","ActionName",initialize); | |
function initialize() { | |
//scripts for the current page go here. | |
} | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment