pathable / supermodel

Supermodel - Minimal Model Tracking for Backbonejs
http://pathable.github.io/supermodel
MIT License
229 stars 38 forks source link

backbonejs version compatibility (backbonejs 1.1) #50

Open fluxsaas opened 10 years ago

fluxsaas commented 10 years ago

Hey,

just notices a broken behavior after upgrading to backbone version 1.1

backbone throws the error Uncaught TypeError: Cannot read property 'cid' of undefined

on line:

https://github.com/jashkenas/backbone/blob/master/backbone.js#L721

after debugging a while i found that fetching records with model.associated_models().fetch() throws this error, after executing 3-4 times :)

it seems that supermodel just adds model after fetching (calling model.associated_models().length shows doubled length after each call) instead of replacing/resetting.

it seems that the new backbone functionality:

If you want to smartly update the contents of a Collection, adding new models, 
removing missing ones, and merging those already present, you now call set 
(previously named "update"), a similar operation to calling set on a Model. This is now 
the default when you call fetch on a collection. To get the old behavior, 
pass {reset: true}.

http://backbonejs.org/#upgrading

breaks supermodel.

i haven't found the right fix for supermodel yet. i gonna try :)

braddunbar commented 10 years ago

Hi @fluxsaas! Thanks for reporting, I'll take a look as well.

fluxsaas commented 10 years ago

debugging further:

with 1.1.0, the 'add' callback is always called after model.associated_models().fetch()

https://github.com/pathable/supermodel/blob/master/supermodel.js#L213

with 1.0.0 it only called on first model.associated_models().fetch()

reverting to the old behavior by passing ´.fetch({reset: true})´ does not through any errors :)

fluxsaas commented 10 years ago

well, i'm stuck. can't figure out the problem. i can confirm that it's not backbone throwing the 'add' event after fetch:

a = new Backbone.Collection()
a.fetch()
a.on('add', function(a){console.log(a)})

does not shows any callback. So sth. on supermodel's end...

a = model.associated_models()
a.on('add', function(a){console.log(a)})
a.fetch()

triggers the 'add' callback.

lapluviosilla commented 10 years ago

Any luck on this? I just upgraded Backbone and am having some of the same issues. Strangely enough, I don't get the TypeError you are getting but I am having issues with duplicate models and collections with exactly double the length.

fluxsaas commented 10 years ago

@lapluviosilla well. no not really. i have posponed this issue for me.

i have the same issue with duplicate models. But only for one specific collection. e.g.:

bad:

users = Account.users()
users.fetch()
users.length #  => 6, should be 3

good:

users = new Users()
users.url = "/api/account/#{@account.id}/users"
users.fetch()
users.length #  => 3

i think it's a config error on the association configuration (.many / .one) on my side. but i havn't figured it out yet....

fluxsaas commented 10 years ago

@lapluviosilla

i might have found the issue. with the above example i have:

association setup:

Account.has()
  .many "users",
User.has()
  .one "account",

with the server side json:

  {
    id: 1
    name: ''Karl'
    account_id: 2
  }

which results in the doublication. If i remove the account_id from the json it works!

maybe it's the same problem?

fluxsaas commented 10 years ago

Hey @braddunbar , i think i figured out the problem. Maybe you can have a look at the failing test in my commit: 74fe5851fbfb49b32ac309e2d9137101ab1d0e1b

jmlamare commented 8 years ago

The pull request https://github.com/pathable/supermodel/pull/69 suggest a fix. I've tested it only with unit tests.