123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- define("dojox/mvc/StatefulModel", [
- "dojo/_base/lang",
- "dojo/_base/array",
- "dojo/_base/declare",
- "dojo/Stateful"
- ], function(lang, array, declare, Stateful){
-
- var StatefulModel = declare("dojox.mvc.StatefulModel", [Stateful], {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- data: null,
-
-
-
-
- store: null,
-
-
-
- valid: true,
-
-
-
- value: "",
-
- reset: function(){
-
-
-
-
- if(lang.isObject(this.data) && !(this.data instanceof Date) && !(this.data instanceof RegExp)){
- for(var x in this){
- if(this[x] && lang.isFunction(this[x].reset)){
- this[x].reset();
- }
- }
- }else{
- this.set("value", this.data);
- }
- },
- commit: function(/*"dojo.store.DataStore?"*/ store){
-
-
-
-
-
-
-
-
-
-
-
- this._commit();
- var ds = store || this.store;
- if(ds){
- this._saveToStore(ds);
- }
- },
- toPlainObject: function(){
-
-
-
-
-
-
-
- var ret = {};
- var nested = false;
- for(var p in this){
- if(this[p] && lang.isFunction(this[p].toPlainObject)){
- if(!nested && typeof this.get("length") === "number"){
- ret = [];
- }
- nested = true;
- ret[p] = this[p].toPlainObject();
- }
- }
- if(!nested){
- if(this.get("length") === 0){
- ret = [];
- }else{
- ret = this.value;
- }
- }
- return ret;
- },
- add: function(/*String*/ name, /*dojo.Stateful*/ stateful){
-
-
-
-
-
-
-
-
-
-
-
-
- var n, n1, elem, elem1, save = new StatefulModel({ data : "" });
- if(typeof this.get("length") === "number" && /^[0-9]+$/.test(name.toString())){
- n = name;
- if(!this.get(n)){
- if(this.get("length") == 0 && n == 0){
- this.set(n, stateful);
- } else {
- n1 = n-1;
- if(!this.get(n1)){
- throw new Error("Out of bounds insert attempted, must be contiguous.");
- }
- this.set(n, stateful);
- }
- }else{
- n1 = n-0+1;
- elem = stateful;
- elem1 = this.get(n1);
- if(!elem1){
- this.set(n1, elem);
- }else{
- do{
- this._copyStatefulProperties(elem1, save);
- this._copyStatefulProperties(elem, elem1);
- this._copyStatefulProperties(save, elem);
- this.set(n1, elem1);
- elem1 = this.get(++n1);
- }while(elem1);
- this.set(n1, elem);
- }
- }
- this.set("length", this.get("length") + 1);
- }else{
- this.set(name, stateful);
- }
- },
- remove: function(/*String*/ name){
-
-
-
-
-
-
-
-
- var n, elem, elem1;
- if(typeof this.get("length") === "number" && /^[0-9]+$/.test(name.toString())){
- n = name;
- elem = this.get(n);
- if(!elem){
- throw new Error("Out of bounds delete attempted - no such index: " + n);
- }else{
- this._removals = this._removals || [];
- this._removals.push(elem.toPlainObject());
- n1 = n-0+1;
- elem1 = this.get(n1);
- if(!elem1){
- this.set(n, undefined);
- delete this[n];
- }else{
- while(elem1){
- this._copyStatefulProperties(elem1, elem);
- elem = this.get(n1++);
- elem1 = this.get(n1);
- }
- this.set(n1-1, undefined);
- delete this[n1-1];
- }
- this.set("length", this.get("length") - 1);
- }
- }else{
- elem = this.get(name);
- if(!elem){
- throw new Error("Illegal delete attempted - no such property: " + name);
- }else{
- this._removals = this._removals || [];
- this._removals.push(elem.toPlainObject());
- this.set(name, undefined);
- delete this[name];
- }
- }
- },
- valueOf: function(){
-
-
-
-
-
- return this.toPlainObject();
- },
- toString: function(){
-
-
-
-
-
- return this.value === "" && this.data ? this.data.toString() : this.value.toString();
- },
-
- constructor: function(/*Object*/ args){
-
-
-
-
-
-
-
-
-
-
-
-
-
- var data = (args && "data" in args) ? args.data : this.data;
- this._createModel(data);
- },
-
- _createModel: function(/*Object*/ obj){
-
-
-
-
-
-
- if(lang.isObject(obj) && !(obj instanceof Date) && !(obj instanceof RegExp) && obj !== null){
- for(var x in obj){
- var newProp = new StatefulModel({ data : obj[x] });
- this.set(x, newProp);
- }
- if(lang.isArray(obj)){
- this.set("length", obj.length);
- }
- }else{
- this.set("value", obj);
- }
- },
- _commit: function(){
-
-
-
-
-
- for(var x in this){
- if(this[x] && lang.isFunction(this[x]._commit)){
- this[x]._commit();
- }
- }
- this.data = this.toPlainObject();
- },
- _saveToStore: function(/*"dojo.store.DataStore"*/ store){
-
-
-
-
-
-
-
-
- if(this._removals){
- array.forEach(this._removals, function(d){
- store.remove(store.getIdentity(d));
- }, this);
- delete this._removals;
- }
- var dataToCommit = this.toPlainObject();
- if(lang.isArray(dataToCommit)){
- array.forEach(dataToCommit, function(d){
- store.put(d);
- }, this);
- }else{
- store.put(dataToCommit);
- }
- },
- _copyStatefulProperties: function(/*dojo.Stateful*/ src, /*dojo.Stateful*/ dest){
-
-
-
-
-
-
-
-
-
- for(var x in src){
- var o = src.get(x);
- if(o && lang.isObject(o) && lang.isFunction(o.get)){
- dest.set(x, o);
- }
- }
- }
- });
- return StatefulModel;
- });
|