base.coffee 857 B

1234567891011121314151617181920212223242526272829303132
  1. Spine = require('spine/core')
  2. class BaseModel extends Spine.Model
  3. @alphaSort: (a, b) ->
  4. if a.name?
  5. if a.name > b.name then 1 else -1
  6. else if a.title?
  7. if a.title > b.title then 1 else -1
  8. @queryOn: ['name','title']
  9. @filter: (obj) ->
  10. return @all() unless obj
  11. query = obj.query.toLowerCase()
  12. siteId = obj.siteId.toLowerCase()
  13. @select (item) =>
  14. # See if matching any of the properties to filter on
  15. matchedQuery = false
  16. for p in @queryOn
  17. matchedQuery = matchedQuery or item[p]?.toLowerCase().indexOf(query) isnt -1
  18. matchedSite = if siteId and item.site then item.site is siteId else true
  19. if query and siteId
  20. matchedQuery and matchedSite
  21. else if query
  22. matchedQuery
  23. else if siteId
  24. matchedSite
  25. else
  26. true
  27. module.exports = BaseModel