Skip to content

Commit

Permalink
various refactors - async data
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Tonge committed Sep 19, 2012
1 parent 4a92ffc commit 407b030
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 89 deletions.
3 changes: 1 addition & 2 deletions attend/client/app/dom/addContactButton.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ cj ->

link.click ->
window.location.hash = "/contact/#{id}"
require "../initialize"
alert "show attendance for cid #{id}"
require "../initialize"
1 change: 0 additions & 1 deletion attend/client/app/dom/addEventButton.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ cj ->
container.find('#sc_attend').click ->
window.location.hash = "/event/#{id}"
require "../initialize"
alert "show attedance for eid: #{id}"
58 changes: 43 additions & 15 deletions attend/client/app/lib/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ contactView = require "../views/contact"
eventDateView = require "../views/eventDate"
eventView = require "../views/event"

class Async
counter:0
add: (name, method) ->
@counter += 1
@fns[name] = method

fns:{}
check: ->
@finish() if @counter is 0

run: (@finish) ->
for name, fn of @fns
do (name, fn) =>
fn (result) =>
@[name] = result
@counter -= 1
@check()


# The Actual Router
Expand All @@ -26,31 +43,42 @@ module.exports = class Router extends Backbone.Router


contactEvent: (cid, eid) ->
model = app.contacts.getOrFetch(cid)
event = app.events.getOrFetch(eid)
async = new Async
async.add "model", (cb) ->
app.contacts.getOrFetch(cid, cb)
async.add "event", (cb) ->
app.events.getOrFetch(eid, cb)
collection = app.records.getByContact(cid)
view = new contactEventView {model, event, collection}
@render view
async.run =>
view = new contactEventView {model:async.model, event:async.event, collection}
@render view


contact: (cid) ->
model = app.contacts.getOrFetch(cid)
async = new Async
async.add "model", (cb) -> app.contacts.getOrFetch(cid, cb)
collection = app.records.getByContact(cid)
view = new contactView {model, collection}
@render view
async.run =>
view = new contactView {model:async.model, collection}
@render view

eventDate: (eid, date) ->
model = app.events.getOrFetch(eid)
async = new Async
async.add "model", (cb) -> app.events.getOrFetch(eid, cb)
collection = app.records.getByEvent(eid).getByDate(date)
view = new eventDateView {model, collection, date}

window.dave3 = {model, collection, date}
@render view
contacts = app.contacts.getByEvent(eid)
async.run =>
view = new eventDateView {model:async.model, collection, contacts, date}
@render view

event: (eid) ->
model = app.events.getOrFetch(eid)
async = new Async
async.add "model", (cb) -> app.events.getOrFetch(eid, cb)
collection = app.records.getByEvent(eid)
view = new eventView {model, collection}
@render view
contacts = app.contacts.getByEvent(eid)
async.run =>
view = new eventView {model:async.model, collection, contacts}
@render view

render: (view) ->
@$el.dialog "close"
Expand Down
18 changes: 13 additions & 5 deletions attend/client/app/models/collection.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# Base class for all collections.
module.exports = class Collection extends Backbone.Collection

getOrFetch: (id) ->
getOrFetch: (id, callback) ->
model = @get id
unless model
if model
callback model
else
model = new @model {id}
model.fetch()
@add model
model
model.fetch
success: =>
cached = @get id
if cached
cached.set model.toJSON()
callback cached
else
@add model
callback model

3 changes: 3 additions & 0 deletions attend/client/app/models/contact.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ module.exports = class ContactModel extends Model
read: "get"
delete: false

defaults:
event_ids:[]


40 changes: 40 additions & 0 deletions attend/client/app/models/contacts.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
Contact = require "./contact"
Collection = require "./collection"
$ = cj
eventFetches = []

module.exports = class Contacts extends Collection
model:Contact

makeChild: ->
child = new Contacts
child.App = @App
child

getByEvent: (event_id) ->
filtered = @makeChild()
if event_id in eventFetches
filtered.add @App.contacts.where {event_id}
else
eventFetches.push event_id
@fetch {event_id}, filtered
filtered


fetch: (query = {}, filtered) =>
base = Drupal.settings.basePath
url = base + "civicrm/ajax/rest"
query.entity = "contact"
query.action = "get"
query.json = 1
$.getJSON url, query, (result) =>
contacts = result.values
if _.isObject(contacts) and not _.isArray(contacts)
for id, contact of contacts
model = @App.contacts.get(contact.id)
if model
model.get("event_ids").push contact.event_id
model.trigger "change", "event_id", contact.event_id
else
contact.event_ids = [contact.event_id]
delete contact.event_id
@App.contacts.add contact
model = @App.contacts.get(contact.id)
@add model unless @get(model.id)
if filtered
filtered.add(model) unless filtered.get(model.id)
24 changes: 20 additions & 4 deletions attend/client/app/models/record.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
Model = require "./model"
noop = ->
stub =
toJSON: noop
get: noop
on: noop

module.exports = class RecordModel extends Model
methodMap:
Expand All @@ -15,11 +20,22 @@ module.exports = class RecordModel extends Model
data.date = data.date.toString("yyyy-MM-dd")
data

relatedContact: stub
relatedEvent: stub



initialize: ->
@relatedEvent = @collection.App.events.getOrFetch(@get "event_id")
@relatedEvent.on "change", => @trigger "change"
@relatedContact = @collection.App.contacts.getOrFetch(@get "contact_id")
@relatedContact.on "change", => @trigger "change"
@collection.App.events.getOrFetch @get("event_id"), (model) =>
@relatedEvent = model
@relatedEvent.on "change", => @trigger "change"
@relatedEvent.trigger "change"

@relatedContact = @collection.App.contacts.getOrFetch @get("contact_id"), (model) =>
@relatedContact = model
@relatedContact.on "change", => @trigger "change"
@relatedContact.trigger "change"




Expand Down
2 changes: 1 addition & 1 deletion attend/client/app/models/records.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = class Records extends Collection

makeChild: ->
child = new Records
child.app = @app
child.App = @App
child

getByContact: (contact_id) ->
Expand Down
6 changes: 5 additions & 1 deletion attend/client/app/views/event.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ template = require "./templates/event"

module.exports = class Event extends View
template:template

init: ->
@contactsOn "add remove", @render

getRenderData: ->
title:"Event Attendence Records"
event:@model.toJSON()
dates:@collection.getDates()
contacts:@collection.getContacts()
contacts:@options.contacts.toJSON()
8 changes: 8 additions & 0 deletions attend/client/app/views/templates/event.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
{{#each dates}}
<li><a href="#/event/{{../event.id}}/{{.}}">{{.}}</a></li>
{{/each}}
</ul>
<a href="#" class="add_date">Add Date</a>
<hr>

<ul>
{{#each contacts}}
<li><a href="#/contact/{{id}}">{{display_name}}</a></li>
{{/each}}
</ul>
8 changes: 6 additions & 2 deletions attend/client/app/views/view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ module.exports = class View extends Backbone.View
initialize: (options) ->
@init(options)
@modelOn "change", @render
@collectionOn "change", @render
@collectionOn "change add remove", @render
@eventOn "change", @render
@collectionOn "all", -> console.log arguments

modelOn: (event, callback) ->
@model?.on event, callback, @
collectionOn: (event, callback) ->
@collection?.on event, callback, @
eventOn: (event, callback) ->
@event?.on event, callback, @
@options.event?.on event, callback, @
contactsOn: (event, callback) ->
@options.contacts?.on event, callback, @

dispose: ->
@model?.off null, null, @
@collection?.off null, null, @
@options.event?.off null, null, @
@options.contact?.off null, null, @
@remove()

getRenderData: ->
Expand Down
Loading

0 comments on commit 407b030

Please sign in to comment.