本文介绍了多个型号的子类的集合Backbone.js的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST API的Json返回一个列表航海日志。有迹象表明,实施不同但类似的行为,许多类型的航海日志。数据库层在服务器端实现的,这是一种单表继承的,所以日志的每一JSON重新presentation包含其类型:

I have a REST Json API that returns a list "logbooks". There are many types of logbooks that implement different but similar behavior. The server side implementation of this on the Database layer is a sort of Single Table Inheritance, so each JSON representation of a logbook contains its "type" :

[
  {"type": "ULM", "name": "My uml logbook", ... , specific_uml_logbook_attr: ...},
  {"type": "Plane", "name": "My plane logbook", ... , specific_plane_logbook_attr: ...}
]

我想复制客户端上的这台服务器的模式,所以我有一个基地日志类和多个日志子类:

class Logbook extends Backbone.Model

class UmlLogbook extends Logbook

class PlaneLogbook extends Logbook

...

我的 Backbone.Collection 是一组日志,我用它来查询JSON API模型:

My Backbone.Collection is a set of Logbook models that i use to query the JSON API :

class LogbookCollection extends Backbone.Collection
  model: Logbook
  url: "/api/logbooks"

当我获取日志的收集,有没有办法施展每个日志其相应的子类(基于JSON的类型属性)?

When I fetch the logbook collection, is there a way to cast each Logbook to its corresponding sub class (based on the JSON "type" attribute) ?

推荐答案

确实存在。

当你调用一个集合'取',它通过Backbone.Collection.parse将它添加到集合之前的响应。

When you call 'fetch' on a collection, it passes the response through Backbone.Collection.parse before adding it to the collection.

解析的默认实现只经过回应,是的,但你可以重写它返回的型号列表中添加到集合:

The default implementation of 'parse' just passes the response through, as is, but you can override it to return a list of models to be added to the collection:

class Logbooks extends Backbone.Collection

  model: Logbook

  url: 'api/logbooks'

  parse: (resp, xhr) ->
    _(resp).map (attrs) ->
      switch attrs.type
        when 'UML' then new UmlLogbook attrs
        when 'Plane' then new PLaneLogbook attrs

编辑:哇,idbentley了那里,在我面前。唯一的区别是,他用每个',我用地图。既会工作,但方式不同。

whoa, idbentley got there before me. the only difference being he used 'each' and I used 'map'. Both will work, but differently.

使用每个'有效地打破了取呼叫开始的链条(通过返回未定义 - 以重置(或添加),因此不会做任何事情的后续调用),并完成所有的处理权有在解析功能。

Using 'each' effectively breaks the chain that the 'fetch' call started (by returning 'undefined' - the subsequent call to 'reset' (or 'add') therefore will do nothing) and does all the processing right there in the parse function.

使用地图只是变换属性列表到模型列表,并将其传递回链已经在运动。

Using 'map' just transforms the list of attributes into a list of models and passes it back to the chain already in motion.

不同中风。

再次编辑:刚刚意识到还有另一种方式来做到这一点:

EDIT AGAIN: just realized there's also another way to do this:

在'模式'对集合属性是有只有这么集合知道如何使一个新的模式,如果它传递添加,创造或者复位的属性。所以,你可以这样做:

The 'model' attribute on a collection is there only so the collection knows how to make a new model if it's passed attributes in 'add', 'create' or 'reset'. So you could do something like:

class Logbooks extends Backbone.Collection

  model: (attrs, options) ->
    switch attrs.type
      when 'UML' then new UmlLogbook attrs, options
      when 'Plane' then new PLaneLogbook attrs, options
      # should probably add an 'else' here so there's a default if,
      # say, no attrs are provided to a Logbooks.create call

  url: 'api/logbooks'

这样做的好处是,现藏会知道如何投工作日志右侧子比'取'等操作。

The advantage of this is that the collection will now know how to 'cast' the right subclass of Logbook for operations other than 'fetch'.

这篇关于多个型号的子类的集合Backbone.js的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 13:42