简体   繁体   English

Backbone.Model.extend不会创建实例属性

[英]Backbone.Model.extend does not create instance properties

I am new to Backbone and JS, so correct me if I am wrong... 我是Backbone和JS的新手,如果我错了,请纠正我......

Using the example from backbone.js site, 使用backbone.js站点的示例,

var Note = Backbone.Model.extend({
    initialize: function() { ... },
    author: function() { ... },
    coordinates: function() { ... },
    allowedToEdit: function(account) {
        return true;
    } 
});

Backbone.js says, Backbone.js说,

"To create a Model class of your own, you extend Backbone.Model and provide instance properties, as >well as optional classProperties to be attached directly to the constructor function." “要创建自己的Model类,可以扩展Backbone.Model并提供实例属性,以及>可选的classProperties直接附加到构造函数。”

So i create two objects, 所以我创建了两个对象,

var n1 = new Note;
var n2 = new Note;

Now the properties autor, allowedToEdit coordinates etc., are not instance properties of n1 and n2. 现在属性autor,allowedToEdit坐标等不是n1和n2的实例属性。 They are created under the proto link. 它们是在proto链接下创建的。 How do I create instance properties in backbone.js 如何在backbone.js中创建实例属性

Also, if I try to modify a inherited property in javascript in an object, the inherited property is not changed. 此外,如果我尝试在对象的javascript中修改继承的属性,则不会更改继承的属性。 Instead, a new property with the same name is created in the objects instance properties. 而是在对象实例属性中创建具有相同名称的新属性。 How do I achive this? 我怎么做到这一点? Thanks in advance... 提前致谢...

I believe you are a bit confused about what extend does. 我相信你对扩展的做法有点困惑。 Extend is analogous to inheritance. Extend类似于继承。 If you extend the Model in this way, you create a derived "class" called Note which now has these member functions. 如果以这种方式扩展Model ,则创建一个名为Note的派生“类”,它现在具有这些成员函数。

But that is not what you want, I don't think. 但这不是你想要的,我不认为。 You want a model with these DATA properties. 您需要具有这些DATA属性的模型。 Adding data to an instance is simple: 向实例添加数据很简单:

var Note = Backbone.Model.extend({});
var n1 = new Note({
    author: "Gautham",
    coordinates: {x: 200, y: 100},
    allowedToEdit: true
});

var author = n1.get("author");

In other words, data properties on Backbone models are dynamic. 换句话说,Backbone模型上的数据属性是动态的。 You don't need to declare them in your Note derivation. 您不需要在Note推导中声明它们。

If, however, you would like the Note derivation to have properties that look this way, you can always define them to proxy through to the get function: 但是,如果您希望Note推导具有这种方式的属性,则可以始终将它们定义为代理到get函数:

var Note = Backbone.Model.extend({
    author: function() { return this.get('author'); },
    coordinates: function() { return this.get('coordinates'); }
    allowedToEdit: function() { return this.get('allowedToEdit'); }
});
var n1 = new Note({...});
var author = n1.author();

You wanted to use defaults attribute, that are the behavior that you are expecting: 您想使用defaults属性,这是您期望的行为:

var Note = Backbone.Model.extend({
  defaults:function(){
    return {
      author : 'Default author',
      coordinates : [0,0],
    };
  }
});

var note1 = new Note;
var note2 = new Note;
var note3 = new Note({author: 'Daniel!', coordinates:[9,9]});

console.log( note1.toJSON() );
console.log( note2.toJSON() );
console.log( note3.toJSON() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM