Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

RestStorage not loading/saving models with belongsTo association #357

Closed
jholster opened this Issue Mar 11, 2012 · 1 comment

Comments

Projects
None yet
1 participant

A model collection with belongsTo association is not loaded by RestStorage on Model.load() or Model.get('all.toArray').

Run the following fiddle and watch for ajax activity. Bar.load() makes a request but Foo.load() does not.

http://jsfiddle.net/JbWCD/57/

Is this correct behaviour or am I missing something?

I made a test case with working REST backend. Seems that belongsTo model is not saved (no POST). I would assume the product to be saved as { category_id: 874f5dfa43047ab219fe000001 }.

class Category extends Model
    @persist RestStorage
    #@hasMany 'products', saveInline: false

class Product extends Model
    @persist RestStorage
    @belongsTo 'category', saveInline: false


cat = new Category()
cat.save(null, ->
    console.log cat.state() # saved
    console.log cat.get 'id' # 874f5dfa43047ab219fe000001
    p = new Product(name: 'foo')
    p.set 'category', cat
    p.save(null, ->
        console.log p.state() # creating <- ???
        console.log p.get 'id' # undefined <- ???
    )
)

If I uncomment the hasMany line, the following error occurs: (batman.jquery.js:5078)

TypeError: 'undefined' is not an object (evaluating 'this.association.getRelatedModel().get')

(Edit: Fixed a bug in test code; moved console.log lines inside p.save() callback, no effect on the output.)

@jholster jholster closed this Apr 1, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment