Skip to content

Commit

Permalink
Allow passing of options (for query string) to model instance methods…
Browse files Browse the repository at this point in the history
… load/save/destroy.
  • Loading branch information
John Lynch committed Dec 22, 2011
1 parent 61392d8 commit ed52d72
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/batman.coffee
Expand Up @@ -2491,21 +2491,27 @@ class Batman.Model extends Batman.Object
hasStorage: -> (@_batman.get('storage') || []).length > 0

# `load` fetches the record from all sources possible
load: (callback) =>
load: (options, callback) =>
if !callback
[options, callback] = [{}, options]

if @state() in ['destroying', 'destroyed']
callback?(new Error("Can't load a destroyed record!"))
return

@loading()
@_doStorageOperation 'read', {}, (err, record) =>
@_doStorageOperation 'read', options, (err, record) =>
unless err
@loaded()
record = @constructor._mapIdentity(record)
callback?(err, record)

# `save` persists a record to all the storage mechanisms added using `@persist`. `save` will only save
# a model if it is valid.
save: (callback) =>
save: (options, callback) =>
if !callback
[options, callback] = [{}, options]

if @state() in ['destroying', 'destroyed']
callback?(new Error("Can't save a destroyed record!"))
return
Expand All @@ -2523,7 +2529,7 @@ class Batman.Model extends Batman.Object
# Save belongsTo models immediately since we don't need this model's id
associations?.getByType('belongsTo')?.forEach (association, label) => association.apply(@)

@_doStorageOperation (if creating then 'create' else 'update'), {}, (err, record) =>
@_doStorageOperation (if creating then 'create' else 'update'), options, (err, record) =>
unless err
if creating
do @created
Expand All @@ -2537,9 +2543,12 @@ class Batman.Model extends Batman.Object
callback?(err, record)

# `destroy` destroys a record in all the stores.
destroy: (callback) =>
destroy: (options, callback) =>
if !callback
[options, callback] = [{}, options]

do @destroying
@_doStorageOperation 'destroy', {}, (err, record) =>
@_doStorageOperation 'destroy', options, (err, record) =>
unless err
@constructor.get('all').remove(@)
do @destroyed
Expand Down

0 comments on commit ed52d72

Please sign in to comment.