forked from koding/koding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectref.coffee
More file actions
executable file
·48 lines (39 loc) · 1.2 KB
/
Copy pathobjectref.coffee
File metadata and controls
executable file
·48 lines (39 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{ dash } = require 'sinkrow'
Model = require './model'
Base = require './base'
ObjectId = require './objectid'
module.exports = class ObjectRef extends Model
@share(true)
@setSchema {
constructorName : String
id : ObjectId
}
# ducktype ObjectRefs
@isObjectRef = (item) -> item.id? and item.constructorName?
@populate = (objectRefs, callback) ->
objects = []
queue = objectRefs.map (objectRef) ->
->
ObjectRef(objectRef).populate (err, object) ->
if err
queue.fin(err)
else
objects.push object
queue.fin()
dash queue, -> callback null, objects
constructor: ->
return new ObjectRef arguments... unless this instanceof ObjectRef
switch arguments.length
when 1
[seed] = arguments
if seed instanceof Model
constructorName = seed.constructor.name
id = seed.getId()
else
{ constructorName, id } = seed
when 2
[constructorName, id] = arguments
super { constructorName, id }
populate:(callback) ->
Base.constructors[@constructorName].one { _id: @id }, callback
Base::toObjectRef = -> ObjectRef(this)