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
autocompleteFormat = (url) -> (request, response) -> $.get (url.replace '%s', encodeURIComponent request.term), response, 'json' | |
# Usage | |
$('#search').autocomplete | |
source: autocompleteFormat '/search/%s.json' |
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
defaults = writable: true, enumerable: true, configurable: true | |
initializer = (key) -> (val) -> | |
prop = Object.create defaults | |
prop[key] = val | |
prop | |
value = initializer 'value' | |
getter = initializer 'get' | |
setter = initializer 'set' |
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
## Version 1 ## | |
# To continue to the next iteration, `return` instead of `continue` | |
# To stop iteration, `throw StopIteration` instead of `break` | |
StopIteration = {} | |
iter = (iterator, func) -> | |
try loop func do iterator | |
catch e then throw e unless e is StopIteration | |
return | |
# Usage |
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
# A simple implementation to get a private storage area for objects. | |
# Also allows setting default properties for the storage area. | |
# Written in CoffeeScript: (with an added `export` function. On the regular version, use `exports.privates = ...`) | |
export privates = do -> | |
counter=0 | |
(proto) -> | |
key = "__id#{ ++counter }" | |
store = [] | |
(object) -> store[object[key] ||= store.length] ||= Object.create proto or null |
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
# Adds a new `default_cols` option under [query] with a comma-separated list of default columns | |
# See http://trac-hacks.org/wiki/DefaultColsPlugin | |
from trac.core import * | |
from trac.web.main import IRequestFilter | |
from trac.config import ListOption | |
class DefaultCols(Component): | |
implements(IRequestFilter) |
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
# A tiny helper for creating objects with dynamic keys. I found myself repeating the following pattern all over my code: | |
doStuff = -> | |
obj = {} | |
obj[k] = foo for k in bar() | |
obj | |
# Which is not very elegant, and in many cases forces me to expand one-line functions into multiple lines. | |
# Instead, its possible to move the repeated pattern into a separate function: |
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
/* | |
* Just some small UI enhancement for multi-projects. In my setup, I have an `project` custom field and | |
* "project/" prefixes for the component, version and milestones fields (e.g. "AwesomeProject/1.2" as a, | |
* version, "SomeProject/User" as a component, etc). This JavaScript hides the irrelevant components/versions/ | |
* milestones on the ticket creation/edit forms according to the selected project, adds some navigation | |
* to the roadmap page to only show milestones of a specific project and creates links for the project-specific | |
* roadmap page (using the hash). | |
*/ | |
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
# With a `returnex` function decorator for returning exception values | |
# Previous versions at https://gist.github.com/1922656 | |
iter = returnex = null | |
do -> | |
StopIteration = {} | |
NextIteration = {} | |
ReturnValue = (@val) -> | |
helpers = | |
continue: -> throw NextIteration |
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
# Allows return values to be returned from anonymous functions within other functions. The function that | |
# the value should be returned from should be decorated with the `catchx` function, and the value should | |
# be returned using the `returnx` function. This is done by throwing the return value using `returnx`, | |
# than catching it inside the decorator and `return`ing it. | |
# Was previously part of a specific solution for iterators at https://gist.github.com/1953057 | |
catchx = returnx = null | |
do -> | |
ReturnValue = (@val) -> |
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
# Just playing around with some ideas... | |
interface = (names...) -> (klass) -> throw new Error "Missing #{n}" for n in names when not klass::[n]?; klass | |
extender = (props) -> (klass) -> klass::[k]=v for k,v of props; klass | |
watchable = interface 'watch', 'unwatch' | |
writable = interface 'write' | |
evented = extender | |
on: (event, callback) -> do stuff | |
trigger: (name, args...) -> do stuff |