Skip to content

Commit 0559b62

Browse files
committed
Closurify.
1 parent b9944fa commit 0559b62

File tree

1 file changed

+109
-107
lines changed

1 file changed

+109
-107
lines changed

src/model_model.js

+109-107
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,127 @@
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+
}
6210

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+
}
6819

69-
save: function(callback) {
70-
if (this.valid()) {
20+
Model.Model.prototype = {
21+
destroy: function(callback) {
7122
var self = this
7223

73-
this.constructor.persistence.save(this, function(success) {
24+
this.constructor.persistence.destroy(this, function(success) {
7425
if (success) {
75-
Model.Utils.extend(self.attributes, self.changes)
76-
self.reset()
77-
self.emit("save", self)
26+
self.emit("destroy", self)
7827
}
7928

80-
if (callback) callback.apply(self, arguments)
29+
if (callback) callback.apply(this, arguments)
8130
})
82-
} else if (callback) {
83-
callback(false);
84-
}
8531

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]
9547
} else {
96-
this.changes[name] = value;
48+
// Combined attributes/changes object.
49+
return Model.Utils.extend({}, this.attributes, this.changes)
9750
}
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)
10485
}
10586

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+
}
10899

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+
}
111106

112-
toJSON: function() {
113-
return this.get()
114-
},
107+
this.emit("change", this)
108+
}
109+
110+
return this
111+
},
115112

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+
},
121116

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+
}
124126
}
125-
};
127+
})(Model);

0 commit comments

Comments
 (0)