Skip to content

Instantly share code, notes, and snippets.

resortView: ->
@collection.each (model, index) =>
view = @children.findByModel(model)
@moveViewToIndex(view, index) if view._index isnt index
moveViewToIndex: (view, index) ->
view._index = index
sibling = view.$el.siblings().eq(index)
view.$el.insertBefore(sibling)
@brian-mann
brian-mann / gist:9696358
Created March 21, 2014 20:58
why doesnt underscore wrap work
Backbone.sync = (method, entity, options = {}) ->
## THIS WORKS
_.each ["beforeSend", "complete", "error"], (fn) ->
options[fn] or= ->
orig = options[fn]
options[fn] = ->
orig.apply(entity, arguments)
methods[fn].apply(entity, arguments)
@brian-mann
brian-mann / gist:8640612
Created January 26, 2014 23:00
Coffeescript classes vs Javascript extend
// All Backbone objects have the extend method built in.
// This serves the same primary purpose as Coffeescript's extends keyword.
// After all, they were actually written by the same person: Jeremey Ashkenas
// Coffeescript Implementation for Backbone Model
class Entities.User extends Backbone.Model
defaults:
firstName: "Brian"
initialize: ->
@brian-mann
brian-mann / gist:7477964
Created November 15, 2013 02:04
automating tagName's and columns for itemViews
class Views.CompositeView extends Marionette.CompositeView
itemViewEventPrefix: "childview"
itemViewOptions: (model, index) ->
options = {}
options.tagName = "tr" if @isTbody()
options.tagName = "li" if @isUl()
options.tableColumns = @$el.find("th").length if @isTbody()
options
@brian-mann
brian-mann / gist:7343058
Created November 6, 2013 19:55
Kendo mixin to automate closing of kendo elements on ui
## this is the mixin
Concerns.Kendo =
onBeforeClose: ->
@destroyKendos()
destroyKendos: ->
for key, el of @ui
widget = kendo.widgetInstance(el, kendo.ui)
widget?.destroy?()
@brian-mann
brian-mann / gist:7085402
Last active September 3, 2017 20:14
The latest and greatest folder and file structure for Backbone / Marionette Apps
##Folder structure
/backbone
app.js
/apps
/entities
/lib
##Inside Apps
/apps
@brian-mann
brian-mann / gist:7085079
Last active July 13, 2023 08:37
Expanded version of loading controller with edge cases covered
showRealView: (realView, loadingView, config) ->
xhrs = App.request "fetched:entities", config.entities
## ...after the entities are successfully fetched, execute this callback
$.when(xhrs...).done =>
## ================================================================ ##
## If the region we are trying to insert is not the loadingView then
## we know the user has navigated to a different page while the loading
## view was still open. In that case, we know to manually close the original
## view so its controller is also closed. We also prevent showing the real
@brian-mann
brian-mann / gist:6923050
Created October 10, 2013 18:21
Clever trick to automate children tagName based on their parent's element
class Views.CompositeView extends Marionette.CompositeView
itemViewEventPrefix: "childview"
itemViewOptions: (model, index) ->
options = {}
options.tagName = "tr" if @itemViewContainer is "tbody"
options
class Views.CollectionView extends Marionette.CollectionView
itemViewEventPrefix: "childview"
@brian-mann
brian-mann / gist:6922710
Last active December 25, 2015 05:09
spying on ui hash
#= require support/spec_helper
class View extends App.Views.ItemView
template: "tooltips"
describe "Tooltip Concerns", ->
beforeEach ->
@view = new View
@view.onRender = =>
@tooltip = sinon.spy(@view.ui.tooltips, "tooltip")
@brian-mann
brian-mann / gist:6904285
Created October 9, 2013 16:42
Kendo Template Helpers
myTemplateHelpers = {
foo: "foo",
bar: function(){}
}
$("#something").kendoTreeView({
templateHelpers: myTemplateHelpers,
template: JST["path/to/template"] // this is a function
})