主干可以将属性包含集合? [英] Backbone can an attribute contain a collection?

查看:95
本文介绍了主干可以将属性包含集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在骨干什么是一位作家和书籍之间存储的关系(在localStorage的)期望的方式。

是否有可能有一个集合作为属性还是应该阵列中使用还是...?

  VAR书= Backbone.Model.extend({
    默认值:{
        标题:''
    }
});VAR书= Backbone.Collection.extend({
    模式:书
});
VAR作者= Backbone.Model.extend({
        默认值:{
        名字: '',
        姓: '',
        书:新书()
        }
});VAR作者= Backbone.Collection.extend({
    型号:作者,
    localStorage的新Backbone.LocalStorage(作家)
});


解决方案

有是在骨干处理嵌套集合没有唯一正确的方法。从骨干常见问题解答报价:


  

骨干不包括嵌套模型和收藏或直接支持有很多,因为联想有一些很好的模式为客户端上的造型结构化数据和骨干应该实施其中任何提供了基础。


常见问题解答还表明一个模式,并提供了对扩展,插件,资源链接页面,它链接到您可以使用嵌套处理模型和模型关系的许多库。

这是说,我有时走近这样的问题:

  VAR作者= Backbone.Model.extend({  初始化:功能(ATTRS,期权){
    //子数组转换成集合,即使解析不叫
    VAR书= ATTRS? attrs.books:[];
    如果(!(书籍的instanceof书籍)){
        this.set(图书,新的图书(书),{沉默:真});
    }
  },  //本本属性并不需要在默认声明
  //因为初始化方法将创建它,如果不通过
  默认值:{
    名字: '',
    姓: ''
  },  //重载解析让服务器发送的JSON是自动
  //从书本阵列藏书解析
  解析:功能(ATTRS){
    attrs.books =新的图书(attrs.books);
    返回ATTRS;
  },  //覆盖的toJSON所以藏书是自动
  //转换为一个数组
  的toJSON:功能(){
    VAR JSON = Backbone.Model.prototype.toJSON.call(本);
    如果(的instanceof图书json.books){
      json.books = json.books.toJSON();
    }
    返回JSON;
  }
});

的意见希望解释它是如何工作的,但问题是,你可以使用该模型,你通常会:有一个集合,数组或没有初始化的孩子,得到了从服务器,将它们发送到服务器,它应该都透明地工作。有相当多的样板code写的,但它是相对简单的抽象为一个基类,如果你发现自己重复同样的code。

编辑:小幅盘整,你不需要图书默认定义对象,因为构造函数,如果缺失创建它。

/ code没有测试样品

In backbone what is the desired way to store the relationship (in localstorage) between an author and books.

Is it possible to have a collection as an attribute or should an array be used or...?

var book = Backbone.Model.extend({ 
    defaults:{ 
        title: ''
    }
});

var books = Backbone.Collection.extend({
    model: book
});


var author = Backbone.Model.extend({
        defaults:{ 
        firstName: '',
        lastName: '', 
        books: new books()
        }
});

var authors = Backbone.Collection.extend({
    model: author,
    localStorage: new Backbone.LocalStorage("authors")
});

解决方案

There is no One True Way of handling nested collections in Backbone. Quoting from Backbone FAQ:

Backbone doesn't include direct support for nested models and collections or "has many" associations because there are a number of good patterns for modeling structured data on the client side, and Backbone should provide the foundation for implementing any of them.

The FAQ also suggests one pattern and provides a link to the Extensions, Plugins, Resources page, which links to many libraries you can use for handling nested models and model relations.

That said, I have sometimes approached the problem like this:

var Author = Backbone.Model.extend({

  initialize: function(attrs, options) {
    //convert the child array into a collection, even if parse was not called
    var books = attrs ? attrs.books : [];
    if(!(books instanceof Books)) {
        this.set('books', new Books(books), {silent:true});
    }
  },

  //the books property does not need to be declared in the defaults
  //because the initialize method will create it if not passed
  defaults: {
    firstName: '',
    lastName: ''    
  },

  //override parse so server-sent JSON is automatically
  //parsed from books array to Books collection
  parse: function(attrs) {
    attrs.books = new Books(attrs.books);
    return attrs;
  },

  //override toJSON so the Books collection is automatically
  //converted to an array 
  toJSON: function() {
    var json = Backbone.Model.prototype.toJSON.call(this);
    if(json.books instanceof Books) {
      json.books = json.books.toJSON();
    }
    return json;
  }
});

The comments hopefully explain how it works, but the point is that you can use the model as you would normally: initialize the children with a collection, an array or nothing, get the from a server, send them to the server, it should all work transparently. There's quite a bit of boilerplate code to write, but it's relatively simple to abstract into a base class, if you find yourself repeating the same code.

Edit: Small correction, you don't need the books defined in the defaults object, because the constructor creates it if missing.

/code sample not tested

这篇关于主干可以将属性包含集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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