Created
January 27, 2012 01:11
-
-
Save pehrlich/1686282 to your computer and use it in GitHub Desktop.
Getting rid of some cruft in backbone
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
# the current setup, for conveience, removes all capital letters from class names, which is not the best. | |
# this could be fixed, but carefully. | |
# remove the default ensure element, we handle this in Base | |
Backbone.View.prototype._ensureElement = () -> | |
class ZenBucket.Views.Base extends Backbone.View | |
initialize: (render) -> | |
# set up sensible defaults | |
unless @el | |
@el = '#' + @className() | |
unless @$el | |
@$el = $(@el) | |
unless @template | |
@template = JST[@resource_className().toLowerCase() + '/' + @className()] | |
unless @callbacks | |
@callbacks = | |
{ | |
success: _.bind((collection, response) -> | |
console.log "generic success callback on, #{this}:", arguments | |
@render() | |
, this) | |
error: _.bind(() -> | |
console.log "generic error callback on, #{this}:", arguments | |
, this) | |
} | |
unless render == false | |
@render() | |
className: () -> | |
# see: https://github.com/jashkenas/coffee-script/issues/494 | |
@__proto__.constructor.name | |
resource_className: () -> | |
#todo: move to resource? | |
@resource().__proto__.constructor.name | |
resource: () -> | |
@collection || @model | |
json: (json) -> | |
json = {} unless json | |
# resource_name = @resource_className().toLowerCase() | |
# unless json[resource_name] | |
json[@resource_className().toLowerCase()] ||= @resource().toJSON() | |
json | |
fetch: () -> | |
@resource().fetch(@callbacks) | |
filled_tmpl: (options) -> | |
console.log "updated options (filled tmpl)", @json(options) | |
@template(@json(options)) | |
render: (options)-> | |
options = {} unless options | |
@$el.html(@filled_tmpl(options)) | |
this | |
class ZenBucket.Models.Base extends Backbone.Model | |
class ZenBucket.Collections.Base extends Backbone.Collection | |
initialize: ()-> | |
name = @__proto__.constructor.name | |
# such as goal | |
ZenBucket.collection_instances[name] = this | |
unless @url | |
@url = '/' + name | |
ZenBucket.collection = (collection_name) -> | |
window.ZenBucket.collection_instances[collection_name] || new window.ZenBucket.Collections[collection_name]({}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment