Trip
Trip

Reputation: 27114

model.toJSON is not a function using Backbone

This :

@model

Returns :

Object { type="conjugation", verb="ser", yo="soy", more...}

But when I try :

@model.toJSON()

I get :

TypeError: this.model.toJSON is not a function

I am trying to eventually complete this line :

$(@el).html(@template(@model.toJSON() ))

So that I can render this object in a Show with my template.

Any recommendations?

Update

Persuant the comments. I have this as a model, but I can see now how they're are not related.

class AiProject.Models.Verb extends Backbone.Model
  paramRoot: 'verb'

I'm going to try and instantiate this type of verb.

class AiProject.Routers.QuestionsRouter extends Backbone.Router
  initialize: (options) ->
    @verb = new AiProject.Models.Verb
    @verb = options.words

And then back to my View :

class AiProject.Views.Questions.ConjugationView extends Backbone.View
  template: JST["backbone/templates/questions/conjugation"]

render: ->
  $(@el).html(@template(@model.toJSON() ))

Still get the same error though..

Upvotes: 0

Views: 3664

Answers (1)

Kevin Peel
Kevin Peel

Reputation: 4090

It looks like you're setting your model correctly at first, then overwriting it with the value options.words.

Instead of this:

class AiProject.Routers.QuestionsRouter extends Backbone.Router
  initialize: (options) ->
    @verb = new AiProject.Models.Verb
    @verb = options.words

Try this:

class AiProject.Routers.QuestionsRouter extends Backbone.Router
  initialize: (options) ->
    @verb = new AiProject.Models.Verb(options.words)

That creates your model and passes in options.words to be set as the model's attributes.

Upvotes: 2

Related Questions