|
1 |
| -Model.Model = function(attributes) { |
2 |
| - this.attributes = Model.Utils.extend({}, attributes) |
3 |
| - this.changes = {} |
4 |
| - this.errors = new Model.Errors(this) |
5 |
| - this.uid = [name, Model.UID.generate()].join("-") |
6 |
| - if (Model.Utils.isFunction(this.initialize)) this.initialize() |
7 |
| - this.emit("initialize", this) |
8 |
| -} |
9 |
| - |
10 |
| -Model.Model.extend = function() { |
11 |
| - var child = Model.Utils.inherits(this) |
12 |
| - child.anyInstance = new Model.EventEmitter() |
13 |
| - child.persistence = Model.NullPersistence |
14 |
| - child.unique_key = "id" |
15 |
| - Model.Utils.extend(child, Model.ClassMethods) |
16 |
| - return child |
17 |
| -} |
18 |
| - |
19 |
| -Model.Model.prototype = { |
20 |
| - destroy: function(callback) { |
21 |
| - var self = this |
22 |
| - |
23 |
| - this.constructor.persistence.destroy(this, function(success) { |
24 |
| - if (success) { |
25 |
| - self.emit("destroy", self) |
26 |
| - } |
27 |
| - |
28 |
| - if (callback) callback.apply(this, arguments) |
29 |
| - }) |
30 |
| - |
31 |
| - return this; |
32 |
| - }, |
33 |
| - |
34 |
| - emit: function() { |
35 |
| - var anyInstance = this.constructor.anyInstance |
36 |
| - anyInstance.emit.apply(anyInstance, arguments) |
37 |
| - Model.EventEmitter.prototype.emit.apply(this, arguments) |
38 |
| - }, |
39 |
| - |
40 |
| - get: function(name) { |
41 |
| - if (arguments.length) { |
42 |
| - // Changes take precedent over attributes. |
43 |
| - return (name in this.changes) ? |
44 |
| - this.changes[name] : |
45 |
| - this.attributes[name] |
46 |
| - } else { |
47 |
| - // Combined attributes/changes object. |
48 |
| - return Model.Utils.extend({}, this.attributes, this.changes) |
49 |
| - } |
50 |
| - }, |
51 |
| - |
52 |
| - id: function() { |
53 |
| - return this.attributes[this.constructor.unique_key]; |
54 |
| - }, |
55 |
| - |
56 |
| - newRecord: function() { |
57 |
| - return this.constructor.persistence.newRecord(this) |
58 |
| - }, |
59 |
| - |
60 |
| - off: Model.EventEmitter.prototype.off, |
61 |
| - on: Model.EventEmitter.prototype.on, |
| 1 | +;(function(Model) { |
| 2 | + Model.Model = function(attributes) { |
| 3 | + this.attributes = Model.Utils.extend({}, attributes) |
| 4 | + this.changes = {} |
| 5 | + this.errors = new Model.Errors(this) |
| 6 | + this.uid = [name, Model.UID.generate()].join("-") |
| 7 | + if (Model.Utils.isFunction(this.initialize)) this.initialize() |
| 8 | + this.emit("initialize", this) |
| 9 | + } |
62 | 10 |
|
63 |
| - reset: function() { |
64 |
| - this.errors.clear(); |
65 |
| - this.changes = {}; |
66 |
| - return this; |
67 |
| - }, |
| 11 | + Model.Model.extend = function() { |
| 12 | + var child = Model.Utils.inherits(this) |
| 13 | + child.anyInstance = new Model.EventEmitter() |
| 14 | + child.persistence = Model.NullPersistence |
| 15 | + child.unique_key = "id" |
| 16 | + Model.Utils.extend(child, Model.ClassMethods) |
| 17 | + return child |
| 18 | + } |
68 | 19 |
|
69 |
| - save: function(callback) { |
70 |
| - if (this.valid()) { |
| 20 | + Model.Model.prototype = { |
| 21 | + destroy: function(callback) { |
71 | 22 | var self = this
|
72 | 23 |
|
73 |
| - this.constructor.persistence.save(this, function(success) { |
| 24 | + this.constructor.persistence.destroy(this, function(success) { |
74 | 25 | if (success) {
|
75 |
| - Model.Utils.extend(self.attributes, self.changes) |
76 |
| - self.reset() |
77 |
| - self.emit("save", self) |
| 26 | + self.emit("destroy", self) |
78 | 27 | }
|
79 | 28 |
|
80 |
| - if (callback) callback.apply(self, arguments) |
| 29 | + if (callback) callback.apply(this, arguments) |
81 | 30 | })
|
82 |
| - } else if (callback) { |
83 |
| - callback(false); |
84 |
| - } |
85 | 31 |
|
86 |
| - return this; |
87 |
| - }, |
88 |
| - |
89 |
| - set: function(name, value) { |
90 |
| - if (arguments.length == 2) { |
91 |
| - // Don't write to attributes yet, store in changes for now. |
92 |
| - if (this.attributes[name] === value) { |
93 |
| - // Clean up any stale changes. |
94 |
| - delete this.changes[name]; |
| 32 | + return this |
| 33 | + }, |
| 34 | + |
| 35 | + emit: function() { |
| 36 | + var anyInstance = this.constructor.anyInstance |
| 37 | + anyInstance.emit.apply(anyInstance, arguments) |
| 38 | + Model.EventEmitter.prototype.emit.apply(this, arguments) |
| 39 | + }, |
| 40 | + |
| 41 | + get: function(name) { |
| 42 | + if (arguments.length) { |
| 43 | + // Changes take precedent over attributes. |
| 44 | + return (name in this.changes) ? |
| 45 | + this.changes[name] : |
| 46 | + this.attributes[name] |
95 | 47 | } else {
|
96 |
| - this.changes[name] = value; |
| 48 | + // Combined attributes/changes object. |
| 49 | + return Model.Utils.extend({}, this.attributes, this.changes) |
97 | 50 | }
|
98 |
| - |
99 |
| - this.emit("change:" + name, this) |
100 |
| - } else if (typeof name == "object") { |
101 |
| - // Mass-assign attributes. |
102 |
| - for (var key in name) { |
103 |
| - this.set(key, name[key]); |
| 51 | + }, |
| 52 | + |
| 53 | + id: function() { |
| 54 | + return this.attributes[this.constructor.unique_key] |
| 55 | + }, |
| 56 | + |
| 57 | + newRecord: function() { |
| 58 | + return this.constructor.persistence.newRecord(this) |
| 59 | + }, |
| 60 | + |
| 61 | + off: Model.EventEmitter.prototype.off, |
| 62 | + on: Model.EventEmitter.prototype.on, |
| 63 | + |
| 64 | + reset: function() { |
| 65 | + this.errors.clear() |
| 66 | + this.changes = {} |
| 67 | + return this |
| 68 | + }, |
| 69 | + |
| 70 | + save: function(callback) { |
| 71 | + if (this.valid()) { |
| 72 | + var self = this |
| 73 | + |
| 74 | + this.constructor.persistence.save(this, function(success) { |
| 75 | + if (success) { |
| 76 | + Model.Utils.extend(self.attributes, self.changes) |
| 77 | + self.reset() |
| 78 | + self.emit("save", self) |
| 79 | + } |
| 80 | + |
| 81 | + if (callback) callback.apply(self, arguments) |
| 82 | + }) |
| 83 | + } else if (callback) { |
| 84 | + callback(false) |
104 | 85 | }
|
105 | 86 |
|
106 |
| - this.emit("change", this) |
107 |
| - } |
| 87 | + return this |
| 88 | + }, |
| 89 | + |
| 90 | + set: function(name, value) { |
| 91 | + if (arguments.length == 2) { |
| 92 | + // Don't write to attributes yet, store in changes for now. |
| 93 | + if (this.attributes[name] === value) { |
| 94 | + // Clean up any stale changes. |
| 95 | + delete this.changes[name] |
| 96 | + } else { |
| 97 | + this.changes[name] = value |
| 98 | + } |
108 | 99 |
|
109 |
| - return this |
110 |
| - }, |
| 100 | + this.emit("change:" + name, this) |
| 101 | + } else if (typeof name == "object") { |
| 102 | + // Mass-assign attributes. |
| 103 | + for (var key in name) { |
| 104 | + this.set(key, name[key]) |
| 105 | + } |
111 | 106 |
|
112 |
| - toJSON: function() { |
113 |
| - return this.get() |
114 |
| - }, |
| 107 | + this.emit("change", this) |
| 108 | + } |
| 109 | + |
| 110 | + return this |
| 111 | + }, |
115 | 112 |
|
116 |
| - valid: function() { |
117 |
| - this.errors.clear(); |
118 |
| - this.validate(); |
119 |
| - return this.errors.size() === 0; |
120 |
| - }, |
| 113 | + toJSON: function() { |
| 114 | + return this.get() |
| 115 | + }, |
121 | 116 |
|
122 |
| - validate: function() { |
123 |
| - return this; |
| 117 | + valid: function() { |
| 118 | + this.errors.clear() |
| 119 | + this.validate() |
| 120 | + return this.errors.size() === 0 |
| 121 | + }, |
| 122 | + |
| 123 | + validate: function() { |
| 124 | + return this |
| 125 | + } |
124 | 126 | }
|
125 |
| -}; |
| 127 | +})(Model); |
0 commit comments