How can I implement the self-joins in Batmanjs?
In rails, as found here, it goes like this:
class Employee < ActiveRecord::Base
has_many :subordinates, class_name: "Employee", foreign_key: "manager_id"
belongs_to :manager, class_name: "Employee"
end
My current Batmanjs equivalent model looks like this:
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"
I think that should work, if you just switch:
has_many
/belongs_to
=>hasMany
/belongsTo
name: "Employees"
=>name: "Employee"
.Also, you may have to add an encoder for
id
with the LocalStorage adapter. LocalStorage converts the value to a string, but batman.js expects an integer, so you have to coerce it back to integer in the encoder.Here's an example of self-joins (you can copy-paste the encoder from there, too):
http://jsbin.com/cukapedo/18/edit
Pasted here for posterity: