1 | define([ |
---|
2 | "../../lib/object", |
---|
3 | "./_View", |
---|
4 | "dojo/_base/array", |
---|
5 | "dojo/_base/declare", |
---|
6 | "dojo/_base/lang", |
---|
7 | "dojo/date/stamp" |
---|
8 | ], function(objectFuns, _View, array, declare, lang, stamp) { |
---|
9 | |
---|
10 | var _Class = declare([_View],{ |
---|
11 | _type: null, |
---|
12 | constructor: function() { |
---|
13 | if ( this._type === null ) { |
---|
14 | throw new Error("You must specify a concrete type."); |
---|
15 | } |
---|
16 | }, |
---|
17 | create: function() { |
---|
18 | var obj = { |
---|
19 | type: this._type |
---|
20 | }; |
---|
21 | console.warn("You may want to override create for concrete types."); |
---|
22 | return obj; |
---|
23 | }, |
---|
24 | _doSerialize: function(obj) { |
---|
25 | obj = lang.clone(obj); |
---|
26 | obj = this._serialize(obj) || obj; |
---|
27 | this._sanitize(obj); |
---|
28 | return obj; |
---|
29 | }, |
---|
30 | _serialize: function(obj) { |
---|
31 | console.warn("You may want to override _serialize for concrete types."); |
---|
32 | }, |
---|
33 | load: function(id) { |
---|
34 | return this._store.get(id) |
---|
35 | .then(lang.hitch(this,'_doDeserialize'), |
---|
36 | lang.hitch(this,'_deserializeError')); |
---|
37 | }, |
---|
38 | save: function(obj) { |
---|
39 | return this._store.put(this._doSerialize(obj)) |
---|
40 | .then(lang.hitch(this,'_doDeserialize'), |
---|
41 | lang.hitch(this,'_deserializeError')); |
---|
42 | }, |
---|
43 | remove: function(objOrId,rev) { |
---|
44 | var id; |
---|
45 | if ( typeof objOrId === "string" ) { |
---|
46 | id = objOrId; |
---|
47 | } else { |
---|
48 | id = this.getId(objOrId); |
---|
49 | rev = this.getRev(objOrId); |
---|
50 | } |
---|
51 | return this._store.remove(id,{headers:{'If-Match':'"'+rev+'"'}}) |
---|
52 | .otherwise(lang.hitch(this,'_deserializeError')); |
---|
53 | }, |
---|
54 | getRev: function(obj) { |
---|
55 | return obj._rev; |
---|
56 | }, |
---|
57 | getName: function() { |
---|
58 | return this._type; |
---|
59 | }, |
---|
60 | getObjectPath: function(idOrObj) { |
---|
61 | return '/'+this._collection+'/'+(typeof idOrObj === "string" ? |
---|
62 | idOrObj : |
---|
63 | this.getId(idOrObj)); |
---|
64 | }, |
---|
65 | _formatDate: function(date) { |
---|
66 | return stamp.toISOString(date,{zulu:true,milliseconds:true}); |
---|
67 | }, |
---|
68 | _sanitize: function(obj) { |
---|
69 | if ( lang.isArray(obj) ) { |
---|
70 | array.forEach(obj,this._sanitize,this); |
---|
71 | } else if ( lang.isObject(obj) ) { |
---|
72 | objectFuns.forEach(obj,function(v,prop){ |
---|
73 | if ( ( v === null || |
---|
74 | v === "" || |
---|
75 | (typeof v === "number" && isNaN(v)) ) && |
---|
76 | prop !== 'subcode' ) // HACK : this hardcoded exclusion for subcode is quite nasty |
---|
77 | { |
---|
78 | delete obj[prop]; |
---|
79 | } else { |
---|
80 | this._sanitize(v); |
---|
81 | } |
---|
82 | |
---|
83 | },this); |
---|
84 | } |
---|
85 | } |
---|
86 | }); |
---|
87 | |
---|
88 | return _Class; |
---|
89 | |
---|
90 | }); |
---|