Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call method '_onModelEvent' of undefined #103

Closed
mattiassvedhem opened this issue Apr 8, 2012 · 7 comments
Closed

Cannot call method '_onModelEvent' of undefined #103

mattiassvedhem opened this issue Apr 8, 2012 · 7 comments

Comments

@mattiassvedhem
Copy link

So I get this error when I'm taking a preexisting model and saving it to a collection:

Uncaught TypeError: Cannot call method '_onModelEvent' of undefined
_.extend.updatebackbone.relational.js:239
Backbone.RelationalModel.Backbone.Model.extend.setbackbone.relational.js:1122
_.extend.save.options.successbackbone.js:385
jQuery.Callbacks.firejquery.js:1047
jQuery.Callbacks.self.fireWithjquery.js:1165
donejquery.js:7400
jQuery.ajaxTransport.send.callback
@collection.create(@model)

The model is originally created from the collection:

@model = new @collection.model()

The collection that I'm adding to has this in it's model:

  relations: [
    type: Backbone.HasMany
    key: 'rows'
    relatedModel: 'sm.Models.Row'
    collectionType: 'sm.Collections.Rows'
    includeInJSON: false
    reverseRelation: {
      key: 'section'
      includeInJSON: 'id'
    }
  ]

The model that I'm adding has this (recursively embeds itself)

  {
    type: Backbone.HasMany
    key: 'rows'
    relatedModel: 'sm.Models.Row'
    collectionType: 'sm.Collections.Rows'
    reverseRelation: {
      key: 'parent'
      includeInJSON: false
    }
  }]

It seems like I get the error when the ajax POST is returned from the server.. Any Ideas why?
Thanks.

@mattiassvedhem
Copy link
Author

I think this is related to #91 since I'm also using coffeescript. When was the change to reverseRelation done?

I've tried one of the workarounds discussed in #91. To initialize a blank placeholder model, however I get this error when I do that:

Uncaught TypeError: Cannot call method 'bind' of undefined
Backbone.Relationbackbone.relational.js:323

@mattiassvedhem
Copy link
Author

Ok, seems like this is not an issue with Coffeescript class syntax, tried to use Backbone.RelationalModel.extend
syntax and get the same error. It's when I get a response back from the server (I respond with the persisted row, maybe I shouldn't).

@mattiassvedhem
Copy link
Author

Ok so in this line var coll = this.getCollection( model ); model is the row model, correctly updated with the server id.
But somehow coll is returned as undefined. this is a Backbone.store object with the correct collections.

So it seems to me that the model is never stored in it's store location

@mattiassvedhem
Copy link
Author

Oh, and it's the same when I'm trying to destroy a model:

Uncaught TypeError: Cannot call method 'unbind' of undefined
  Backbone.Relational.store.getCollection( this.instance )
  .unbind( 'relational:remove', this._modelRemovedFromCollection );

@DouweM
Copy link
Collaborator

DouweM commented Apr 10, 2012

Try walking through #getCollection() to see why it returns undefined and not the collection!

@PaulUithol
Copy link
Owner

Are you getting anywhere with this? It's a bit hard to debug for us by just getting a description of the symptoms.. You could try isolating your issue into a jsfiddle? Maybe you'll find your problem as well by cutting your code down for that.

@mattiassvedhem
Copy link
Author

The error was on my part, I used _.bindAll @ and didn't explicitly tell which methods to bind, resulting in the getCollection method (I guess) being bound to the view.

Thanks for an awesome project, keep it up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants