Saurabh Kumar
Saurabh Kumar

Reputation: 16651

TypeError: this.model.toJSON is not a function

I am getting the above title error in firebug. Not sure what can be the problem..

My View

app.View.TrendsView = Backbone.View.extend({
        template: _.template($('#trends-template').html()),

        render: function() {
            this.$el.empty();
            this.$el.html(this.template({
                trends: this.model.toJSON()
            }));
            return this.$el;
        }
    });

calling

app.views.trends = new app.View.TrendsView({ model : model });

Firebug

enter image description here

My model creation

      var m = new app.Model.TrendModel();
      m.url='/trends'
      m.set('login', login);
      m.save(null, {
        success: function(collection, model, response){
            app.views.trends = new app.View.TrendsView({
                model : model
            });
            $('#tab-content').empty();
            $('#tab-content').append(app.views.trends.render());
        },
        error: function(collection, model, response){
        }
      });
      m.destroy();

Spring code

@RequestMapping(value = "/trends", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public TrendResultDTO getTrends(@RequestBody UserDTO user,
            HttpServletResponse response) {

Upvotes: 1

Views: 5148

Answers (1)

nikoshr
nikoshr

Reputation: 33334

From the documentation on model.save

save accepts success and error callbacks in the options hash, which will be passed the arguments (model, response, options)

but the arguments your callbacks expect are collection, model, response which means you pass your response as model to your view, as demonstrated by your console log.

Try

m.save(null, {
    success: function(model, response, options){
        app.views.trends = new app.View.TrendsView({
            model : model
        });
        // ...
    },
    error: function(model, response, options){
    }
});

Upvotes: 1

Related Questions