在 Backbone 中处理不可保存的值 [英] Dealing with non-saveable values in Backbone

查看:19
本文介绍了在 Backbone 中处理不可保存的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Backbone 中是否有处理不可保存值的标准方法.

Is there a standard way to deal with non-saveable values in Backbone.

例如

MyModel = Backbone.extend(Backbone.Model, {
    initialize: function () {
        this.set({'inches': this.get('mm') / 25});
    }
})

如果我在这个模型上调用 save() ,它会抛出一个错误,因为 inches 没有对应的数据库字段.我可以想出几种方法来解决这个问题,但我想知道是否有一种久经考验的方法通常最适合用于此问题?

If I call save() on this model it will throw an error as there is no corresponding database field for inches. I can think of a few ways to fix this, but am wondering if there's a tried and tested approach generally best used for this?

目前我的首选解决方案是扩展 Backbone 的 toJSON 方法并允许传递布尔参数 dontCleanup 以允许它仍然返回所有模型的值(包括不可保存的)在需要时,例如用于传递给模板.

At the moment my preferred solution is to extend Backbone's toJSON method and to allow passing of a boolean parameter dontCleanup to allow for it to still return all the model's values (including the non saveable ones) when it's needed e.g. for passing to a template.

推荐答案

我喜欢 Peter Lyon 的想法.我想过几次,但从未真正付诸实施.不过,对于我处理这个问题的所有方式,以下是我最喜欢的两个:

I like Peter Lyon's idea. I've thought about that a few times, but never actually put it in place. For all the ways that I have handled this, though, here are my two favorites:

  • 非属性"值
  • 查看模型

这很简单:不要在模型的标准属性中存储您需要的值.相反,将其直接附加到对象:

This one is simple: don't store the values you need in the model's standard attributes. Instead, attach it directly to the object:


myModel.someValue = "some value";

这里的大问题是您没有获得与在模型上调用 set 相关的所有事件.所以我倾向于用一种为我做所有事情的方法来包装它.比如我在models上放的一个常用方法是select表示这个model已经被选中了:

The big problem here is that you don't get all of the events associated with calling set on the model. So I tend to wrap this up in a method that does everything for me. For example, a common method I put on models is select to say that this model has been selected:


MyModel = Backbone.Model.extend({
  select: function(){
    if (!this.selected){
      this.selected = true;
      this.trigger("change:selected", this, this.selected);
    }
  }
});

就您而言,我不确定这是否是一个好方法.您的数据需要根据属性中已有的值进行计算.

In your case, I'm not sure this would be a good approach. You have data that needs to be calculated based on the values that are in your attributes already.

为此,我倾向于使用视图模型.

For that, I tend to use view models.

基本思想是像往常一样创建一个可持久化的主干模型.但是,您来创建另一个模型,该模型继承您的原始模型并添加您需要的所有数据.

The basic idea is that you create a backbone model that is persist-able, as you normally would. But the you come along and create another model that inherits from your original one and adds all the data that you need.

有很多方法可以做到这一点.这可能是一个非常简单的版本:

There are a very large number of ways that you can do this. Here's what might be a very simple version:


MyModel = Backbone.Model.Extend({ ... });

MyViewModel = function(model){
  var viewModel = Object.create(model);

  viewModel.toJSON = function(){
    var json = model.toJSON();
    json.inches = json.mm / 25;
    return json;
  };

  return viewModel;
});

使用 Object.create 包装它的最大好处是您现在拥有原型继承情况,因此模型中的所有标准功能仍然存在.我们刚刚覆盖了视图模型上的 toJSON 方法,以便它返回带有 inches 属性的 JSON 对象.

The big benefit of wrapping this with Object.create is that you now have a prototypal inheritance situation, so all of your standard functionality from the model is still in place. We've just overridden the toJSON method on the view model, so that it returns the JSON object with the inches attribute.

然后在需要它的视图中,您将模型包装在初始化函数中:

Then in a view that needs this, you would wrap your model in the initialize function:


MyView = Backbone.View.extend({
  initialize: function(){
    this.model = MyViewModel(this.model);
  },

渲染:函数(){var data = this.model.toJSON();//返回英寸}});

render: function(){ var data = this.model.toJSON(); // returns with inches } });

如果你愿意,你可以调用 new MyViewModel(this.model) ,但这不会做任何不同的事情,最后,因为我们从 MyViewModel 函数.

You could call new MyViewModel(this.model) if you want, but that's not going to do anything different, in the end, because we're explicitly returning an object instance from the MyViewModel function.

当您的视图的渲染方法调用 toJSON 时,您将获得 inches 属性.

When your view's render method calls toJSON, you'll get the inches attribute with it.

当然,这个实现有一些潜在的内存问题和性能问题,但是这些问题可以通过一些更好的视图模型代码轻松解决.不过,这个快速而肮脏的例子应该会让你走上正轨.

Of course, there are some potential memory concerns and performance concerns with this implementation, but those can be solved easily with some better code for the view model. This quick and dirty example should get you down the path, though.

这篇关于在 Backbone 中处理不可保存的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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