Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
[feature] migrate Contacts.fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Noëlie Andrieu committed Jan 19, 2017
1 parent 690f58b commit c764f86
Show file tree
Hide file tree
Showing 14 changed files with 2,368 additions and 21 deletions.
17 changes: 11 additions & 6 deletions app/application.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ class Application extends Mn.Application

@contacts = new ContactsCollection()
@filtered = new FilteredCollection()
@tags = new TagsCollection(@contacts)

# As long as Tags require Contacts to be loaded (see Tags.refs), we
# trigger all TagsCollection fetch after syncing ContactsCollection.
@listenToOnce @contacts, 'sync': -> @tags.underlying.fetch reset: true
@tags = new TagsCollection @contacts

@layout = new AppLayout model: @model
@router = new Router()
Expand All @@ -49,10 +45,19 @@ class Application extends Mn.Application

Object.freeze @ if typeof Object.freeze is 'function'


# render components when app starts
onStart: ->
@layout.render()
@contacts.fetch reset: true

callback = (models, collection) =>
console.log 'SUCCESS', models

# As long as Tags require Contacts to be loaded (see Tags.refs), we
# trigger all TagsCollection fetch after syncing ContactsCollection.
# @tags.underlying.fetch {reset: true}

@contacts.fetch {reset: true}, callback

# prohibit pushState because URIs mapped from cozy-home rely on
# fragment
Expand Down
26 changes: 25 additions & 1 deletion app/collections/contacts.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Contact = require 'models/contact'
module.exports = class Contacts extends Backbone.Collection

model: Contact
url: 'contacts'

sortDisabled: false

Expand All @@ -20,6 +19,31 @@ module.exports = class Contacts extends Backbone.Collection
@listenToOnce @, 'sync', -> @contactListener.watch @


fetch: (options={}, success = -> ) ->
# This event is listened by layout
# to display applications views
@trigger 'sync', options

@reset() if options.reset

# FIXME: remove this line when
# cozy.query will works
@add []

success @models, @

# cozy.init { isV2: true }
# cozy.defineIndex 'io.cozy.contacts', ['id']
# .then (index) =>
# cozy.query index, { selector: id: {"$gte": " "} }
# .then (result=[]) =>
# @add result
# success result
# , (err) =>
# success null, err



# Turns a vcard text into a list of contacts. Then it saves every contacts
# in the data system. Finally it reloads the contact list.
importFromVCF: (vcard) ->
Expand Down
1 change: 1 addition & 0 deletions app/collections/filtered.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = class FilteredCollection

query: null


constructor: ->
app = require 'application'

Expand Down
10 changes: 7 additions & 3 deletions app/collections/tags.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Tags extends Backbone.Collection

model: require 'models/tag'

url: 'tags'


initialize: ->
@listenToOnce @, 'sync', -> (new TagsListener()).watch @
Expand All @@ -27,6 +25,12 @@ class Tags extends Backbone.Collection
_.uniq tags, (tag) -> tag.name.toLowerCase()


fetch: (success) ->
# TODO: add fake data
# TODO: add cozy-client-js code
console.log "(fetch) TAGS"


# Export TagsCollection is the Contacts aware projection. All tags Collection
# can be accessed at `underlying` property.
module.exports = class FilteredTags extends Filtered
Expand All @@ -49,7 +53,7 @@ module.exports = class FilteredTags extends Filtered
# concact tags.
updateRefs: ->
@refs = @contacts.chain()
.map (model) -> model.get 'tags'
.map (model) -> model.get('tags') or []
.flatten()
.uniq()
.value()
Expand Down
3 changes: 2 additions & 1 deletion app/models/config.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = class Config extends Backbone.Model

url: 'config'
fetch: (success) ->
console.log "(fetch) CONFIG"
10 changes: 6 additions & 4 deletions app/models/contact.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ app = undefined


class Datapoint extends Backbone.Model

defaults:
name: 'other'
type: 'other'
Expand All @@ -11,6 +12,7 @@ class Datapoint extends Backbone.Model
# represents an e-mail address from that of a cloud.
mediatype: ''


parse: (attrs) ->
if attrs.name is 'adr' and _.isArray attrs.value
attrs.value = VCardParser.adrArrayToString attrs.value
Expand All @@ -19,8 +21,6 @@ class Datapoint extends Backbone.Model

module.exports = class Contact extends Backbone.Model

urlRoot: 'contacts'

defaults:
n: ';;;;'

Expand Down Expand Up @@ -86,7 +86,8 @@ module.exports = class Contact extends Backbone.Model


validate: (attrs, options) ->
# Handle specific attributes.
console.log "PLOP", attrs, options
Handle specific attributes.
attrs.fn = VCardParser.nToFN attrs.n.split ';'

datapoints = (attrs.datapoints?.toJSON() or [])
Expand Down Expand Up @@ -151,7 +152,7 @@ module.exports = class Contact extends Backbone.Model
# We override toJSON to ensure the exported object is the a true object.
# Allow a options.toVCF boolean to sanitize the output before.
toJSON: (options) ->
_.extend super, datapoints: @attributes.datapoints.toJSON()
_.extend super, datapoints: @attributes.datapoints?.toJSON()


toHighlightedString: (pattern, opts= {})->
Expand Down Expand Up @@ -239,6 +240,7 @@ module.exports = class Contact extends Backbone.Model


toVCF: (callback) ->
console.log "TO_VCF"
data = @toJSON()

# Because viewModel call uses the saveAs method, that broke console
Expand Down
1 change: 0 additions & 1 deletion app/models/tag.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = class Tag extends Backbone.Model

urlRoot: 'tags'

parse: (attrs) ->
attrs.name = attrs.name.toLowerCase()
Expand Down
2 changes: 1 addition & 1 deletion app/routes/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ module.exports = class Router extends Backbone.Router
settings: ->
app.model.set 'dialog', 'settings'


legacyContactRedirect: (slug) ->
@navigate "contacts/#{slug}", trigger: true

5 changes: 3 additions & 2 deletions app/views/app_layout.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module.exports = class AppLayout extends Mn.LayoutView
'select:end': 'hideContextualMenu'
'change:scored': 'onSearchResults'


childEvents:
'drawer:toggle': 'toggleDrawer'
'contacts:select': (contactsView, id) -> @model.select id
Expand Down Expand Up @@ -225,5 +226,5 @@ module.exports = class AppLayout extends Mn.LayoutView
if err is ERR.SEARCH_TOO_SHORT
@results.$el.attr 'aria-hidden', true
@ui.counter
.text t 'error search too short'
.addClass 'important'
.text t 'error search too short'
.addClass 'important'
1 change: 0 additions & 1 deletion app/views/contacts/index.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ContactViewModel = require 'views/models/contact'
ContactRowView = require 'views/contacts/row'
Slugifier = require 'lib/slugifier'

Expand Down
1 change: 1 addition & 0 deletions app/views/models/app.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = class AppViewModel extends Backbone.ViewModel
app = require 'application'

@set 'selected', []

@listenTo @, 'change:filter', (nil, value) ->
@unselectAll()
@set 'scored', CONFIG.search.pattern('text').test value
Expand Down
4 changes: 3 additions & 1 deletion app/views/models/contact.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = class ContactViewModel extends Backbone.ViewModel
new: false
edit: false


map:
avatar: '_attachments'
name: 'n'
Expand All @@ -22,6 +23,7 @@ module.exports = class ContactViewModel extends Backbone.ViewModel
save: 'onSave'
reset: 'onReset'


viewEvents:
'edit:cancel': 'reset'
'tags:update': 'updateTags'
Expand Down Expand Up @@ -105,7 +107,7 @@ module.exports = class ContactViewModel extends Backbone.ViewModel

getIndexKey: ->
sortIdx = if app.model.get('sort') is 'fn' then 0 else 1
initial = @get('initials')[sortIdx]?.toLowerCase() or ''
initial = @get('initials')?[sortIdx]?.toLowerCase() or ''
if /[a-z]/.test initial then initial else '#'


Expand Down
1 change: 1 addition & 0 deletions config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports.config =
'vendor/marionette/backbone.radio.js'
'vendor/marionette/backbone.marionette.js'
'vendor/marionette/radio.shim.js'
'vendor/cozy-client-0.0.1.js'
]

stylesheets:
Expand Down
Loading

0 comments on commit c764f86

Please sign in to comment.