-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dave Tonge
committed
Sep 19, 2012
1 parent
4a92ffc
commit 407b030
Showing
12 changed files
with
419 additions
and
89 deletions.
There are no files selected for viewing
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
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
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
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
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 | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,7 @@ module.exports = class ContactModel extends Model | |
read: "get" | ||
delete: false | ||
|
||
defaults: | ||
event_ids:[] | ||
|
||
|
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
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) |
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
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
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
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
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
Oops, something went wrong.