自我加入Batmanjs

时间:2014-06-29 21:58:38

标签: self-join batman.js

如何在Batmanjs中实现自连接?

在rails中,如here所示,它是这样的:

class Employee < ActiveRecord::Base
  has_many :subordinates, class_name: "Employee", foreign_key: "manager_id"
  belongs_to :manager, class_name: "Employee"
end

我现在的Batmanjs等效模型看起来像这样:

class Employee extends Batman.Model
  @resourceName: 'employees'
  @storageKey: 'employees'

  @persist Batman.LocalStorage

  @has_many 'subordinates', name: "Employees", foreignKey: "manager_id"
  @belongs_to 'manager', name: "Employee"

1 个答案:

答案 0 :(得分:1)

我认为如果你只是转换,那应该有用:

  • has_many / belongs_to =&gt; hasMany / belongsTo
  • name: "Employees" =&gt; name: "Employee"

此外,您可能必须使用LocalStorage适配器为id添加编码器。 LocalStorage将值转换为字符串,但batman.js需要一个整数,因此您必须将其强制转换回编码器中的整数。

以下是自连接的示例(您也可以从那里复制粘贴编码器):

http://jsbin.com/cukapedo/18/edit

粘贴给后人:

class App.Color extends Batman.Model 
  @resourceName: 'color'
  @persist Batman.LocalStorage
  @encode 'name', 'source_color_id'
  # required for numbers in localStorage:
  @encode 'id', 
    encode: (val) -> +val
    decode: (val) -> +val

  @hasMany 'child_colors', name: 'Color', foreignKey: 'source_color_id'
  @belongsTo 'source_color', name: 'Color'