如何更新现有模型的属性?

Ama*_*eur 4 backbone.js

我想更新从另一个视图传递的现有模型的rank属性.但是,我收到错误Uncaught TypeError:Object#没有方法'set'.

在视图的初始化部分,我有:

this.collection = new tgcollection({model : this.options.model });
Run Code Online (Sandbox Code Playgroud)

我定义了一个函数updateModel,旨在将属性值更新为:

updateModel: function(){
    var val= $("#textbox_id").val();
console.log(val);   
console.log(JSON.stringify(this.options.model));
JSON.stringify(this.options.model);
this.options.model.set({"rank": val});
this.render();
//
},
Run Code Online (Sandbox Code Playgroud)

我哪里错了?我可以在控制台中看到值和模型及其先前的属性值.

该模型:

define(['jquery','underscore', 'backbone', 'deepmodel'], 
       function($,_, Backbone) {
         var model = Backbone.DeepModel.extend({

        // Default attributes for the model.
        defaults : {
            id: null,
                rank: null,

        },

        initialize: function(){
            _.bindAll(this,"update");
                    this.bind('change : cost', this.update);
        },

        update: function(){
            console.log(this.get("cost"));

        },
        // Remove this model from *localStorage*.
        clear : function() {
            this.destroy();
        },

    });
    return model;
Run Code Online (Sandbox Code Playgroud)

});

sch*_*cki 11

做就是了

this.model.set({"rank": val});
Run Code Online (Sandbox Code Playgroud)

代替

this.options.model.set({"rank": val});
Run Code Online (Sandbox Code Playgroud)

视图中的模型是通过this.model而不是this.options.model访问的