Skip to content

Instantly share code, notes, and snippets.

View rmosolgo's full-sized avatar
🧀

Robert Mosolgo rmosolgo

🧀
View GitHub Profile
@rmosolgo
rmosolgo / parent.coffee
Last active August 29, 2015 13:57
encode child items from ids
class Parent extends Batman.Model
# Encode 'children' from JSON like {children: [1,2,3]}
@encode 'children',
# to put the child ids in JSON, just pull out the ides and set them on the JSON object
encode: (value, key, builtJSON, record) ->
ids = value.mapToProperty('id')
builtJSON.key = ids
# to load records from JSON, make a new Set and fill it with child records
@rmosolgo
rmosolgo / index.haml
Last active August 29, 2015 13:57
Infinite scroll in batman.js
#helpdesk_search_results{"data-event-scroll" => "checkScroll"}
.doc_search_container
%ul.lesson_container
%li{"data-foreach-lesson" => "Lesson.loaded"}
%div{"data-partial" => "lessons/_lesson"}
@rmosolgo
rmosolgo / model_transaction.coffee
Last active August 29, 2015 13:57
Model::transaction
# from https://github.com/batmanjs/batman/pull/992/
# example:
#
# class App.ThingsController extends Batman.Controller
# edit: (params) ->
# App.Thing.find params.id, (err, record) =>
# @set('thing', record.transaction())
#
# save: ->
# @get('thing').save() # The `transaction` implements ::save which applies changes and fires save on the original.
@rmosolgo
rmosolgo / observe_loaded.coffee
Last active August 29, 2015 13:57
Observing a loaded set in batman.js
class App.Wizard extends Batman.Model
loadedSet = App.Wizard.get('loaded')
loadedSet.on 'itemsWereAdded', (items, indexes) ->
# items is an array of the new records
console.log("#{items.length} new wizards!")
loadedSet.on 'itemsWereRemoved', (items, indexes) ->
@rmosolgo
rmosolgo / toJSON.coffee
Created March 25, 2014 20:08
override Model::toJSON to support accepts_nested_attributes_for
class App.Model extends Batman.Model
toJSON: ->
builtJSON = super
builtJSON.nested_items_attributes = builtJSON.nested_items
delete builtJSON.nested_items
builtJSON
@rmosolgo
rmosolgo / model observer.coffee
Created April 10, 2014 22:32
Observe attributes of a batmanjs Batman.Model
class MyApp.Thing extends Batman.Model
constructor: ->
super
# Whenever someProperty changes, capitalized property will be updated
@observe 'someProperty', (newValue, oldValue) ->
@set('capitalizedProperty', newValue?.toUpperCase())
@rmosolgo
rmosolgo / Gulpfile.js
Created April 13, 2014 23:35
Render batman.js views into a modal
var gulp = require('gulp');
var coffee = require('gulp-coffee');
var concat = require('gulp-concat');
var jade = require('gulp-jade');
var batmanTemplates = require("gulp-batman-templates")
gulp.task('default', function(){
gulp.watch('./**/*', ["build", "html", "finalize"])
});
Loader =
show: ->
hide: ->
window.activeXhrCount = 0
window.globalLoaderEnabled = true
$(document).ajaxSend ->
window.activeXhrCount++
Loader.show()
.ajaxComplete ->
@rmosolgo
rmosolgo / select_2_view.coffee
Created April 15, 2014 17:36
Batman.js Select2 Custom View
# Props to @theberg for making this
#
# usage:
#
# select data-view='Select2View' data-view-bind='item.option_id'
# option value='' None
# option data-foreach-opt='Option.all' data-bind='opt.name' data-bind-value='opt.id'
#
class App.Select2View extends Batman.View
@option 'bind', 'placeholder'
@rmosolgo
rmosolgo / json_storage.coffee
Last active August 29, 2015 13:59
Append `.json` to URLs for storage operations
# Taken from Batman.RailsStorage https://github.com/batmanjs/batman/blob/master/src/extras/batman.rails.coffee#L62
#
# Usage:
# #= require ./json_storage
# class MyApp.Model extends Batman.Model
# @persist Batman.JSONStorage
#
class Batman.JSONStorage extends Batman.RestStorage
# override the default URL functions to add .json:
urlForRecord: -> @_addJsonExtension(super)