Skip to content

Instantly share code, notes, and snippets.

@pirhoo
Created September 16, 2013 11:33
Show Gist options
  • Save pirhoo/6579546 to your computer and use it in GitHub Desktop.
Save pirhoo/6579546 to your computer and use it in GitHub Desktop.
# Thanks to http://stackoverflow.com/questions/12506329/how-to-dynamically-change-header-based-on-angularjs-partial-view
# ──────────────────────────────────────────────────────────────────────────────
# Service
# ──────────────────────────────────────────────────────────────────────────────
angular.module('myApp').factory "Page", ->
title = "..."
#
#* To Title Case 2.0.1 – http://individed.com/code/to-title-case/
#* Copyright © 2008–2012 David Gouch. Licensed under the MIT License.
#
toTitleCase = (str)->
smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i
str.replace /([^\W_]+[^\s-]*) */g, (match, p1, index, title) ->
return match.toLowerCase() if index > 0 and index + p1.length isnt title.length and p1.search(smallWords) > -1 and title.charAt(index - 2) isnt ":" and title.charAt(index - 1).search(/[^\s-]/) < 0
return match if p1.substr(1).search(/[A-Z]|\../) > -1
match.charAt(0).toUpperCase() + match.substr(1)
title : ->title
setTitle: (newTitle, titleCase=true)->
title = if titleCase then toTitleCase(newTitle) else newTitle
# ──────────────────────────────────────────────────────────────────────────────
# Controller
# ──────────────────────────────────────────────────────────────────────────────
class PageCtrl
# Injects dependancies
@$inject: ['$scope', 'Page']
constructor: (@scope, @Page)-> @scope.Page = @Page
angular.module('myApp').controller 'pageCtrl', PageCtrl
# ──────────────────────────────────────────────────────────────────────────────
# Template
# ──────────────────────────────────────────────────────────────────────────────
# <title ng-bind="Page.title() + ' - Detective.io'">Detective.io</title>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment