1 | define([ |
---|
2 | "dojo/_base/array", |
---|
3 | "dojo/_base/lang", |
---|
4 | "dojo/Stateful", |
---|
5 | "./StatefulArray" |
---|
6 | ], function(array, lang, Stateful, StatefulArray){ |
---|
7 | var equalsOptions = { |
---|
8 | // summary: |
---|
9 | // Options used for dojox/mvc/equals(). |
---|
10 | |
---|
11 | getType: function(/*Anything*/ v){ |
---|
12 | // summary: |
---|
13 | // Returns the type of the given value. |
---|
14 | // v: Anything |
---|
15 | // The value. |
---|
16 | |
---|
17 | return lang.isArray(v) ? "array" : lang.isFunction((v || {}).getTime) ? "date" : v != null && ({}.toString.call(v) == "[object Object]" || lang.isFunction((v || {}).set) && lang.isFunction((v || {}).watch)) ? "object" : "value"; |
---|
18 | }, |
---|
19 | |
---|
20 | equalsArray: function(/*Anything[]*/ dst, /*Anything[]*/ src){ |
---|
21 | // summary: |
---|
22 | // Returns if the given two stateful arrays are equal. |
---|
23 | // dst: Anything[] |
---|
24 | // The array to compare with. |
---|
25 | // src: Anything[] |
---|
26 | // The array to compare with. |
---|
27 | |
---|
28 | for(var i = 0, l = Math.max(dst.length, src.length); i < l; i++){ |
---|
29 | if(!equals(dst[i], src[i])){ return false; } |
---|
30 | } |
---|
31 | return true; |
---|
32 | }, |
---|
33 | |
---|
34 | equalsDate: function(/*Date*/ dst, /*Date*/ src){ |
---|
35 | return dst.getTime() == src.getTime(); |
---|
36 | }, |
---|
37 | |
---|
38 | equalsObject: function(/*Object*/ dst, /*Object*/ src){ |
---|
39 | // summary: |
---|
40 | // Returns if the given two stateful objects are equal. |
---|
41 | // dst: Object |
---|
42 | // The object to compare with. |
---|
43 | // src: Object |
---|
44 | // The object to compare with. |
---|
45 | |
---|
46 | var list = lang.mixin({}, dst, src); |
---|
47 | for(var s in list){ |
---|
48 | if(!(s in Stateful.prototype) && s != "_watchCallbacks" && !equals(dst[s], src[s])){ return false; } |
---|
49 | } |
---|
50 | return true; |
---|
51 | }, |
---|
52 | |
---|
53 | equalsValue: function(/*Anything*/ dst, /*Anything*/ src){ |
---|
54 | // summary: |
---|
55 | // Returns if the given two values are equal. |
---|
56 | |
---|
57 | return dst === src; // Boolean |
---|
58 | } |
---|
59 | }; |
---|
60 | |
---|
61 | var equals = function(/*Anything*/ dst, /*Anything*/ src, /*dojox/mvc/equalsOptions*/ options){ |
---|
62 | // summary: |
---|
63 | // Compares two dojo/Stateful objects, by diving into the leaves. |
---|
64 | // description: |
---|
65 | // Recursively iterates and compares stateful values. |
---|
66 | // dst: Anything |
---|
67 | // The stateful value to compare with. |
---|
68 | // src: Anything |
---|
69 | // The stateful value to compare with. |
---|
70 | // options: dojox/mvc/equalsOptions |
---|
71 | // The object that defines how two stateful values are compared. |
---|
72 | // returns: Boolean |
---|
73 | // True if dst equals to src, false otherwise. |
---|
74 | |
---|
75 | var opts = options || equals, types = [opts.getType(dst), opts.getType(src)]; |
---|
76 | return types[0] != types[1] ? false : opts["equals" + types[0].replace(/^[a-z]/, function(c){ return c.toUpperCase(); })](dst, src); // Boolean |
---|
77 | }; |
---|
78 | |
---|
79 | // lang.setObject() thing is for back-compat, remove it in 2.0 |
---|
80 | return lang.setObject("dojox.mvc.equals", lang.mixin(equals, equalsOptions)); |
---|
81 | }); |
---|