Skip to content

Commit

Permalink
Custom Primary Keys should not encoded with undefined if not
Browse files Browse the repository at this point in the history
set a value.
  • Loading branch information
hookercookerman committed Feb 15, 2012
1 parent 99e571c commit 9b445c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/batman.coffee
Expand Up @@ -2698,7 +2698,7 @@ class Batman.Model extends Batman.Object
obj[key] = encodedVal

if @constructor.primaryKey isnt 'id'
obj[@constructor.primaryKey] = @get('id')
obj[@constructor.primaryKey] = @get('id') unless @get('id') is undefined
delete obj.id

obj
Expand Down
9 changes: 9 additions & 0 deletions tests/batman/model/encoders_test.coffee
Expand Up @@ -82,6 +82,15 @@ test "key ending with ? marked for decoding should be decoded", ->
p.fromJSON(json)
ok p.get('broken?'), "Cool Snowboard"

QUnit.module "Batman.Model: encoding/decoding to/from JSON with custom primary Key"
setup: ->
class @Product extends Batman.Model
@primaryKey: '_id'

test "undefined primaryKey shouldn't be encoded", ->
p = new @Product
deepEqual p.toJSON(), {}

QUnit.module "Batman.Model: encoding: custom encoders/decoders"
setup: ->
class @Product extends Batman.Model
Expand Down

1 comment on commit 9b445c8

@tdegrunt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, will help me!

Please sign in to comment.