UNPKG

backbone-nested-model

Version:

Configure Backbone.Model with heirarchial data

1 lines 3.94 kB
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("backbone"),require("underscore"));else if("function"==typeof define&&define.amd)define(["backbone","underscore"],t);else{var n="object"==typeof exports?t(require("backbone"),require("underscore")):t(e.Backbone,e._);for(var i in n)("object"==typeof exports?exports:e)[i]=n[i]}}(this,function(e,t){return function(e){function t(i){if(n[i])return n[i].exports;var r=n[i]={exports:{},id:i,loaded:!1};return e[i].call(r.exports,r,r.exports,t),r.loaded=!0,r.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";var i=n(2),r=n(1),s=n(3);e.exports=r.Model.extend({schema:{},children:null,constructor:function(e,t){t||(t={}),t.parse=!0,this.children={},r.Model.call(this,e,t)},parse:function(e){var t=i.keys(this.schema),n=i.reduce(t,i.bind(this.mapSchema,this),e);return n},mapSchema:function(e,t){var n,r,o;return r=e[t],!r&&this.defaults&&this.defaults[t]&&(r=i.clone(this.defaults[t])),r&&(n=this.schema[t],o=new n(r),o.on("all",function(e){var n=i.rest(Array.prototype.slice.call(arguments,0),1),r=[].concat([s.modifyEvent(e,t)],n);this.trigger.apply(this,r),"destroy"!==e&&-1===e.indexOf(":")&&this.trigger.apply(this,[].concat([e],n))},this),o.on("change",function(e){this.attributes[t]=i.clone(e.attributes),this.changed[t]=e.changed},this),o.on("destroy",function(){delete this.attributes[t],delete this.schema[t]},this),this.children[t]=o,e[t]=o.toJSON()),e},get:function(e){return s.getChildAttribute(e,this.children)||this.attributes[e]},set:function(e,t,n){var o,c,a,h,u,l;if(null===e)return this;"object"==typeof e?(o=e,n=t):(o={})[e]=t,n||(n={});var d={};for(l in o)if(c=s.getChildKey(l),a=s.getChild(c.key,this.children),h=a,u=o[l],-1!==l.indexOf(".")||-1!==l.indexOf("["))h=s.getChildContext(a,c.relativeKey,u),h.child.set(h.values,n),delete o[l],o[c.key]=a.toJSON(),d[c.key]=h.child.collection?[h.child.changed]:h.child.changed,n.unset=!1;else{if(!this.children.hasOwnProperty(l)&&this.schema.hasOwnProperty(l)){var f=this.schema[l];this.children[l]=new f}if(this.children.hasOwnProperty(l)){a=this.children[l];var p=i.clone(o[l]);a instanceof r.Collection?(a.reset(p),d[l]=i.clone(p)):(a.set(p,n),d[l]=a.changed),null!==p||"undefined"==typeof p?n.unset=!1:n.unset===!0&&delete this.children[l],o[l]=a.toJSON()}}var g=r.Model.prototype.set.apply(this,[o,n]);return this.changed=i.extend({},this.changed,d),g},toJSON:function(){var e,t;e=r.Model.prototype.toJSON.apply(this,arguments);for(t in this.children)e[t]=this.children[t].toJSON();return e},validate:function(){return s.validateInSchema.apply(this,arguments)}})},function(t){t.exports=e},function(e){e.exports=t},function(e,t,n){"use strict";var i=n(2),r=n(1);t.modifyEvent=function(e,t){var n=e.split(":");return[].concat([n[0],t],i.rest(n,1)).join(":")},t.validateInSchema=function(e,t){var n,i,r,s,o=[];for(r in this.schema)n=this.children[r],n&&"function"==typeof n.validate&&(s=n.validate,i=s.call(n,e[r],t),i&&o.push(i));return o.length?o:void 0},t.getChildAttribute=function(e,t){var n,i,r,s,o,c,a,h;if(n=e.indexOf("."),i=e.indexOf("["),r=-1!==n&&(i>n||-1===i),s=-1!==i&&(n>i||-1===n),r)return a=e.substring(0,n),h=t[a].get(e.substring(n+1));if(s){if(o=e.indexOf("]",i),c=e.substring(i+1,o),a=e.substring(0,i),t.hasOwnProperty(a)){h=t[a];var u=h.at(c);if(!u)return;return e=e.substring(o+1),-1===n?u.toJSON():u.get(e.substring(e.indexOf(".")+1))}}else;},t.getChildContext=function(e,t,n){var i,s,o,c,a;return e instanceof r.Collection&&(s=t.indexOf("]"),o=parseInt(t.substring(1,s),10),i=e.at(o)),-1!==t.indexOf(".")&&(c=t.substring(t.indexOf(".")+1),a={},a[c]=n,n=a),{child:i||e,values:n}},t.getChildKey=function(e){var t,n,i,r,s;return t=e.indexOf("."),n=e.indexOf("["),i=-1!==t&&(n>t||-1===n),r=-1!==n&&(t>n||-1===t),s=i?e.substring(0,t):r?e.substring(0,n):e,{key:s,relativeKey:s!==e?e.replace(s,""):e}},t.getChild=function(e,t){return t[e]}}])});