TypeError:this.model.toJSON不是函数

时间:2014-04-02 23:37:54

标签: javascript json backbone.js

我在firebug中收到上述标题错误。不确定可能是什么问题..

我的观点

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;
        }
    });

致电

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

萤火虫

enter image description here

我的模型创建

      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();

春季代码

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

1 个答案:

答案 0 :(得分:1)

来自model.save

上的文档
  

save接受选项哈希中的successerror回调   将通过参数 (model, response, options)

但回调所期望的参数为collection, model, response,这意味着您将响应作为模型传递给视图,如控制台日志所示。

尝试

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