Last active
August 29, 2015 14:05
-
-
Save jibwa/b4caaddec691d1e36ecc to your computer and use it in GitHub Desktop.
Coffee and Angular with Inheritance.
This file contains hidden or 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
app = angular.module('lcs.entityViews') | |
# File entity_controller.coffee | |
# The entity controller expects a meta property and auto-updes | |
# Use the factory to create the class which will be extended | |
# This makes it possible to be injected and extend in any file on the filesystem | |
app.factory 'EntityController', (MetaProvider) -> | |
class EntityController | |
entity: null | |
constructor: (@type) -> | |
MetaProvider.set( | |
@entity.meta() | |
) | |
# File course_page_controller.coffee | |
# Notice we are injecting EntityController here the extending it so that we are using DI properly | |
app.controller 'coursePageController', ($scope, $routeParams, EntityController, Catalog) -> | |
class CoursePageController extends EntityController | |
constructor: -> | |
@entity = Catalog.findEntity('courses', 'title_underscored', $routeParams.title) | |
$scope.entity = @entity | |
$scope.text = "SOME TEXT" | |
super('course') | |
new CoursePageController() | |
# File course_page_controller.coffee | |
app.controller 'modulePageController', ($scope, $routeParams, EntityController, Catalog) -> | |
class ModulePageController extends EntityController | |
constructor: -> | |
@entity = Catalog.findEntity('course_modules', 'title_underscored', $routeParams.module_title) | |
$scope.entity = @entity | |
super('module') | |
new ModulePageController() | |
# File meta_logic.coffee | |
# This code is included to demonstrate howfunction hooks can help decouple view and service logic | |
app.service('MetaProvider', ($location, $rootScope) -> | |
class MetaProvider | |
set: (meta) -> | |
if meta | |
@titleFunction(meta.title) | |
@descriptionFunction(meta.description) | |
@keywordsFunction(meta.keywords) | |
else | |
@titleFunction(@title) | |
@descriptionFunction(@description) | |
@descriptionFunction(@keywords) | |
titleFunction:null | |
descriptionFunction:null | |
keywords:null | |
# Store the original values for title and description | |
title:null | |
description:null | |
new MetaProvider() | |
) | |
app.directive 'titleMeta', (MetaProvider) -> | |
link: (scope, element, attrs) -> | |
MetaProvider.title = element.html() | |
MetaProvider.titleFunction = (title) -> | |
element.html(title) | |
app.directive 'descriptionMeta', (MetaProvider) -> | |
link: (scope, element, attrs) -> | |
MetaProvider.description = element.prop('content') | |
MetaProvider.descriptionFunction = (description) -> | |
element.prop('content', description) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment