javascript - Parsing data in Backbone.js -


i've got number of backbone models have number of nested sub-models. solution looks this:

models.base = backbone.model.extend ({     relatedmodels:  {},      /**      * parses data sent according list of related models.      *      * @since                   version 1      * @param {object} response response      * @return {object}         parsed data      */     parse: function (response) {         var key,             embeddedclass,             embeddeddata;          (key in this.relatedmodels) {             embeddedclass = this.relatedmodels[key];             embeddeddata = response[key];             response[key] = new embeddedclass (embeddeddata, { parse: true });         }          return response;     } }); 

(using stuff gleaned post - nested models in backbone.js, how approach)

this works fine whilst i'm getting stuff server:

models.individual = models.base.extend({     idattribute:    "idind",     urlroot:    "data/individuals/save",      relatedmodels:  {         details:        collections.detaillist,         relationships:  collections.relationshiplist     } }); 

... when try , initialise model plain bit of json, example if this:

var ind = new models.individual ({     idind: 1,     name: "bob holness",     details: [         { option: "i'd 'e' please, bob" },         { option: "can have 'p' please, bob" }     ],     relationships: [] }); 

... doesn't seem want parse "details". i'd guess because it's not running parse function, anyway - how can parse data in both instances?

the easiest way pass parse: true constructor, so:

var ind = new models.individual ({     idind: 1,     ... }, { parse: true }); 

if lot can override constructor in base class , make pass parse: true every time create new model instance:

models.base = backbone.model.extend({     constructor: function(attributes, options) {         var opts = $.extend({}, options || {});         if (_.isundefined(opts.parse)) {             opts.parse = true;         }         backbone.model.call(this, attributes, opts);     },      ... }); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -