声明中的骨干模型变量没有设置默认值 [英] Declaring variables on a backbone model without setting defaults

查看:126
本文介绍了声明中的骨干模型变量没有设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始接触Backbone.js的,我正在寻找一个模型宣布领域的一种方式,而不必提供默认值。这真的只是作为参考,所以,当我开始创建的情况下,我可以看到我需要初始化哪些领域。

I'm just starting out with backbone.js and I'm looking for a way of declaring fields on a model without having to provide defaults. It's really just for reference, so that when I start creating instances, I can see what fields I need to initialize.

使用类似Java的我会写

With something like java I'd write

public class CartLine{
    StockItem stockItem;
    int quantity;

    public int getPrice(){
        return stockItem.getPrice() * quantity;
    }

    public int getStockID(){
        //
    }
}

然而,随着骨干机型,我在引用我的方法的领域,但我实际上没有宣布他们 - 它看起来像我可以很容易地创建一个 CartLine 对象不包含 stockItem 属性或属性。感觉奇怪不,当我宣布该对象提到的字段。特别作为对象是应该重新present在服务器上的一个实体。

However with backbone models, I'm referencing the fields in my method's but I'm not actually declaring them - It looks like I could easily create a CartLine object that doesn't contain a stockItem attribute or a quantity attribute. It feels strange not to mention the fields when I declare the object. Particularly as the object is supposed to represent an entity on the server.

var CartLine = Backbone.Model.extend({

  getStockID: function(){
    return this.stockItem.id;
  },

  getTotalPrice: function() {
    return this.quantity * this.StockItem.get('price');
  }
});

我想我可以通过验证添加某种参考 -

I guess I can add some sort of reference by using validate -

CartLine.validate = function(attrs){
  if (!(attrs.stockItem instanceof StockItem)){
    return "No Valid StockItem set";
  }
  if (typeof attrs.quantity !== 'number'){
    return "No quantity set";
  }
}

不过,我的问题是 - 我失去的东西吗?是否有一个既定的模式为这个?

But my question is - am I missing something? Is there an established pattern for this?

推荐答案

默认是真正的域或数据传输来回从服务器JSON的一部分。

The defaults are really for "fields" or data that is transferred back and forth from the server as part of the json.

如果你只是想创建一些成员变量的模型,该模型是专有的,不会被来回发送到服务器的一部分,那么你可以声明他们)在对象本身或b)上初始化法(施工期间的称呼),他们可以为OPTS的一部分传递:

If you just want to create some member variables as part of the Model, which are proprietary and not going to be sent back and forth to the server, then you can declare them a) on the object itself or b) in the initialize method (called during construction), and they can be passed in as part of opts:

var Widget = Backbone.Model.extend({

    widgetCount: 0,

    defaults: {
        id: null,
        name: null
    }

    initialize: function(attr, opts) {
       // attr contains the "fields" set on the model
       // opts contains anything passed in after attr
       // so we can do things like this
       if( opts && opts.widgetCount ) {
          this.widgetCount = opts.widgetCount;
       }
    }
});

var widget = new Widget({name: 'the blue one'}, {widgetCount: 20});

请记住,如果你在类中声明的对象或数组,它们基本上是常数,并改变它们将修改所有实例:

Keep in mind that if you declare objects or arrays on the class, they are essentially constants and changing them will modify all instances:

var Widget = Backbone.Model.extend({

    someOpts: { one: 1, two: 2},

    initialize: function(attr, opts) {
       // this is probably not going to do what you want because it will
       // modify `someOpts` for all Widget instances.
       this.someOpts.one = opts.one; 
    }
});

这篇关于声明中的骨干模型变量没有设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆