backbone.js - convert super model into inner model -


i have backbone models hierarchy like

modela = backbone.model.extend({     initialize : function(){          this.set("prop1",10);     } });  modelb = modela.extend({     this.set("prop2",new modelc()) }); modeld = modela.extend({     this.set("prop3",new modele()) }); 

modelc , modele backbone models.

i wanted store modelb , modeld objects 1 collection so

i created collection like

collection = backbone.collection({    model : modela }); 

here i'm doing -

  1. i store modela objects collection

  2. converting collection json.

  3. recreating collection json.

here i'm getting modela objects collection want objects of specific type modelb, modeld etc.

how can achieve this?

you can trick creating correct model @mcgarnagle said providing attribute specifies type specify custom constructor collection,

collection = backbone.collection({    model : function(attrs, options){         if(!attrs || !attrs.modeltype)           return new modela(attrs, options)          if(attrs.modeltype === 'modelb')           return new modelb(attrs, options)          //etc...    } }); 

Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -