Skip to content

Commit

Permalink
Merge branch '0.1'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Model.coffee
  • Loading branch information
sderickson committed Nov 14, 2015
2 parents 8f245f5 + a676093 commit ef145d4
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 146 deletions.
6 changes: 0 additions & 6 deletions app/models/User.coffee

This file was deleted.

7 changes: 6 additions & 1 deletion app/templates/home-view.jade
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ block content
li.list-group-item
a(href="/test/client") Client Tests
li.list-group-item
a(href="/test/server") Server Tests
a(href="/test/server") Server Tests


button.btn.btn-default#open-sample-modal-btn Open SampleModal


13 changes: 13 additions & 0 deletions app/templates/sample-modal.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.modal-dialog
.modal-content
.modal-header
button.close(data-dismiss="modal", aria-label="Close")
span(aria-hidden="true")!= '×'
h4.modal-title Sample Modal

.modal-body
p Modal body

.modal-footer
button.btn.btn-default(data-dismiss="modal") Cancel
button.btn.btn-primary(data-dismiss="modal") Okay
11 changes: 10 additions & 1 deletion app/views/HomeView.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
SampleModal = require('views/SampleModal')

class HomeView extends FrimFram.RootView
events:
'click #open-sample-modal-btn': 'onClickOpenSampleModalButton'

template: require 'templates/home-view'

module.exports = HomeView
onClickOpenSampleModalButton: ->
modal = new SampleModal()
modal.show()

module.exports = HomeView
2 changes: 2 additions & 0 deletions app/views/SampleModal.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = class SampleModal extends FrimFram.Modal
template: require('templates/sample-modal')
1 change: 1 addition & 0 deletions bin/dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./node_modules/.bin/brunch watch --server
12 changes: 0 additions & 12 deletions bin/ff-client-test-runner

This file was deleted.

9 changes: 0 additions & 9 deletions bin/ff-server-test-runner

This file was deleted.

6 changes: 3 additions & 3 deletions config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ exports.config =

framework: 'backbone'

plugins:
autoReload:
delay: 300
server:
command: 'node_modules/.bin/nodemon . -e "coffee,js" --watch server --watch app/schemas'

plugins:
coffeelint:
pattern: /^app\/.*\.coffee$/
options:
Expand Down
6 changes: 3 additions & 3 deletions src/Collection.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class Collection extends Backbone.Collection

dataState: 'standby' # or 'fetching'
state: 'standby' # or 'fetching'

constructor: (models, options) ->
super(models, options)
if options?.defaultFetchData
@defaultFetchData = options.defaultFetchData

fetch: (options) ->
@dataState = 'fetching'
@state = 'fetching'
options = FrimFram.wrapBackboneRequestCallbacks(options)
if @defaultFetchData
options.data ?= {}
Expand All @@ -18,4 +18,4 @@ class Collection extends Backbone.Collection
# At some later point, create save, patch, destroy methods?


FrimFram.BaseCollection = FrimFram.Collection = Collection
FrimFram.BaseCollection = FrimFram.Collection = Collection
43 changes: 43 additions & 0 deletions src/Modal.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Modal extends FrimFram.View
@visibleModal: null

className: 'modal fade'
destroyOnHidden: true

onRender: ->
super()

# proxy Bootstrap events to Backbone View events
modal = @
@$el.on 'show.bs.modal', (event) ->
modal.trigger 'show', event
@$el.on 'shown.bs.modal', (event) ->
modal.trigger 'shown', event
modal.onInsert()
@$el.on 'hide.bs.modal', (event) ->
modal.trigger 'hide', event
@$el.on 'hidden.bs.modal', (event) ->
modal.trigger 'hidden', event
modal.onHidden()
@$el.on 'loaded.bs.modal', (event) ->
modal.trigger 'loaded', event

hide: (fast) ->
@$el.removeClass('fade') if fast
@$el.modal('hide')

show: (fast) ->
Modal.visibleModal?.hide(true)
@render()
@$el.removeClass('fade') if fast
$('body').append @$el
@$el.modal('show')
Modal.visibleModal = @

toggle: -> @$el.modal('toggle')

onHidden: ->
Modal.visibleModal = null if Modal.visibleModal is @
@destroy() if @destroyOnHidden

FrimFram.Modal = Modal
39 changes: 0 additions & 39 deletions src/ModalView.coffee

This file was deleted.

10 changes: 5 additions & 5 deletions src/Model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class Model extends Backbone.Model
@on 'add', @onAdded, @
@on 'invalid', @onInvalid, @

dataState: 'standby' # or 'fetching', 'saving'
state: 'standby' # or 'fetching', 'saving'

created: -> new Date(parseInt(@id.substring(0, 8), 16) * 1000)

onAdded: -> @dataState = 'standby'
onAdded: -> @state = 'standby'

schema: ->
s = @constructor.schema
Expand All @@ -21,7 +21,7 @@ class Model extends Backbone.Model
console.debug "\t", error.dataPath, ':', error.message

set: (attributes, options) ->
if (@dataState isnt 'standby') and not (options.xhr or options.headers)
if (@state isnt 'standby') and not (options.xhr or options.headers)
throw new Error('Cannot set while fetching or saving.')

return super(attributes, options)
Expand All @@ -35,12 +35,12 @@ class Model extends Backbone.Model
save: (attrs, options) ->
options = FrimFram.wrapBackboneRequestCallbacks(options)
result = super(attrs, options)
@dataState = 'saving' if result
@state = 'saving' if result
return result

fetch: (options) ->
options = FrimFram.wrapBackboneRequestCallbacks(options)
@dataState = 'fetching'
@state = 'fetching'
return super(options)


Expand Down
16 changes: 8 additions & 8 deletions src/Router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class Router extends Backbone.Router
window.location.reload(true)

routeDirectly: (path, args) ->
return document.location.reload() if @currentView?.reloadOnClose
leavingMessage = _.result(@currentView, 'onLeaveMessage')
return document.location.reload() if @view?.reloadOnClose
leavingMessage = _.result(@view, 'onLeaveMessage')
if leavingMessage
if not confirm(leavingMessage)
return @navigate(this.currentPath, {replace: true})
return @navigate(this.path, {replace: true})

path = "views/#{path}"

Expand All @@ -31,17 +31,17 @@ class Router extends Backbone.Router
@closeCurrentView()
view.render()
$('body').empty().append(view.el)
@currentView = view
@currentPath = document.location.pathname + document.location.search
@view = view
@path = document.location.pathname + document.location.search
view.onInsert()

closeCurrentView: -> @currentView?.destroy()
closeCurrentView: -> @view?.destroy()

setupOnLeaveSite: ->
window.addEventListener "beforeunload", (e) =>
leavingMessage = _.result(@currentView, 'onLeaveMessage')
leavingMessage = _.result(@view, 'onLeaveMessage')
if leavingMessage
e.returnValue = leavingMessage
return leavingMessage

FrimFram.Router = Router
FrimFram.Router = Router
2 changes: 1 addition & 1 deletion src/View.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class View extends Backbone.View
if _.isString(@template) then @template else @template(@getContext())

initContext: (pickPredicate) ->
context = {}
context = { view: @ }
context.pathname = document.location.pathname # ex. '/play/level'
context = _.defaults context, View.globalContext
context = _.extend context, _.pick(@, pickPredicate, @) if pickPredicate
Expand Down
6 changes: 3 additions & 3 deletions src/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ window.FrimFram = {
options ?= {}
originalOptions = _.clone(options)
options.success = (model) ->
model.dataState = 'standby'
model.state = 'standby'
originalOptions.success?(arguments...)
options.error = (model) ->
model.dataState = 'standby'
model.state = 'standby'
originalOptions.error?(arguments...)
options.complete = (model) ->
model.dataState = 'standby'
model.state = 'standby'
originalOptions.complete?(arguments...)
return options
}
28 changes: 14 additions & 14 deletions test/app/frimfram/Collection.spec.coffee
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
describe 'Collection', ->
describe 'dataState', ->
describe 'state', ->
it 'is "fetching" while the collection is being fetched, "standby" otherwise', ->
Collection = FrimFram.Collection.extend({
url: '/db/thingies'
})
collection = new Collection()
expect(collection.dataState).toBe("standby")
expect(collection.state).toBe("standby")
collection.fetch()
expect(collection.dataState).toBe("fetching")
expect(collection.state).toBe("fetching")
request = jasmine.Ajax.requests.mostRecent()
request.respondWith({status: 200, responseText: '[]'})
expect(collection.dataState).toBe("standby")
expect(collection.state).toBe("standby")
collection.fetch()
expect(collection.dataState).toBe("fetching")
expect(collection.state).toBe("fetching")
request = jasmine.Ajax.requests.mostRecent()
request.respondWith({status: 404, responseText: '{}'})
expect(collection.dataState).toBe("standby")
expect(collection.state).toBe("standby")

it 'is set before any given success or error callback is called', ->
Collection = FrimFram.Collection.extend({
Expand All @@ -26,13 +26,13 @@ describe 'Collection', ->
collection.fetch({
success: ->
calls += 1
expect(collection.dataState).toBe("standby")
expect(collection.state).toBe("standby")
})
jasmine.Ajax.requests.mostRecent().respondWith({status: 200, responseText: '[]'})
collection.fetch({
error: ->
calls += 1
expect(collection.dataState).toBe("standby")
expect(collection.state).toBe("standby")
})
jasmine.Ajax.requests.mostRecent().respondWith({status: 404, responseText: '[]'})
expect(calls).toBe(2)
Expand Down Expand Up @@ -60,31 +60,31 @@ describe 'Collection', ->
expect(_(request.url).contains('foo=BAR')).toBe(true)

describe 'fetch(options)', ->
it 'calls options.success and options.error after dataState is back to "standby"', ->
it 'calls options.success and options.error after state is back to "standby"', ->
count = 0
callback = (c) ->
expect(c.dataState).toBe('standby')
expect(c.state).toBe('standby')
count += 1

Collection = FrimFram.Collection.extend({
url: '/db/thingies'
})
c = new Collection()

expect(c.dataState).toBe('standby')
expect(c.state).toBe('standby')

c.fetch({ success: callback })
expect(c.dataState).toBe('fetching')
expect(c.state).toBe('fetching')
request = jasmine.Ajax.requests.mostRecent()
request.respondWith({status: 200, responseText: '{}'})

c.fetch({ complete: callback })
expect(c.dataState).toBe('fetching')
expect(c.state).toBe('fetching')
request = jasmine.Ajax.requests.mostRecent()
request.respondWith({status: 200, responseText: '{}'})

c.fetch({ error: callback })
expect(c.dataState).toBe('fetching')
expect(c.state).toBe('fetching')
request = jasmine.Ajax.requests.mostRecent()
request.respondWith({status: 404, responseText: '{}'})

Expand Down
Loading

0 comments on commit ef145d4

Please sign in to comment.