抽出新建类的函数,可以同时返回多个model

This commit is contained in:
绯村月
2014-02-19 22:08:49 +08:00
parent 975288afa2
commit d34cdba42f

View File

@@ -9,6 +9,15 @@ module.exports = class Model
@create : ->
throw 'this function must be overrided'
@createModels : (docs)->
throw 'docs must be array' if not _.isArray docs
results = []
if docs.length is 1
for doc in docs
results.push @create doc
else
results = @create docs[0]
results
@table : ->
"#{@name.toLowerCase()}s"
@@ -27,12 +36,7 @@ module.exports = class Model
@collection().insert data, {w:1}, (err, docs) =>
throw err if err
if callback
results = []
if docs.length is 1
for doc in doc
results.push @create doc
else
results = @create docs[0]
results = @constructor.createModels docs
callback err, results
update : (newObj , opts = {},callback) ->
@@ -47,12 +51,7 @@ module.exports = class Model
@collection().find(data, opts).toArray (err, docs)=>
throw err if err
if callback
results = []
if docs.length is 1
results = @create docs[0]
else
for doc in docs
results.push @create doc
results = @constructor.createModels docs
callback err, results
@findById: (id, callback) ->