Archive

Posts Tagged ‘javascript’

bootstrap-scrollspy

August 17th, 2013 No comments

There are plenty of broken jsfiddles for this, so to save time, here is some code that works: boostrap-scrollspy

or preview (or download) here

Categories: Development Tags: , , ,

Angular Services in CoffeeScript

August 15th, 2013 1 comment

This jsbin demonstrates how to use a CoffeeScript class to implement an Angular Service, using Service, Factory and Provider styles. You may have to click “Run with JS” to update Output pane. Download gist from github.

Update: by itself this is not enough, since services usually have to inherit other resources, and look something like this:


angular.module("app")
.service('thumbService', ['$log', '$q', ($log, $q) ->
...

A working example (using .factory instead of .service) from the angular-grunt-coffeescript seed app:

###
Example of a service shared across views.
Wrapper around the data layer for the app. 
###
name = 'common.services.dataSvc'

class DataSvc

	constructor: (@$log, @$http, @env) ->

	_get: (relPath)->
		return @$http.get("#{@env.serverUrl}/#{relPath}")

	getPeople: () ->
		return @_get('people')

	getPerson: (id) ->
		return @_get("person/#{id}")

angular.module(name, []).factory(name, ['$log','$http', 'common.services.env', ($log, $http, env) ->
	new DataSvc($log, $http, env)
])