forked from nathanathan/Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagCollection.js
65 lines (54 loc) · 1.93 KB
/
TagCollection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
define(['backbone', 'underscore', 'sfsf'],
function(Backbone, _, sfsf) {
var Tag = Backbone.Model.extend({
defaults: function() {
return {
_timestamp : new Date()
};
},
sync: function(){},
//JSON stringifies dates in a way that the Date object can't parse in webkits.
//Try: new Date(JSON.parse(JSON.stringify(new Date())))
//To avoid this issue dates can be stringified with the String function.
//Use .toUTCString instead?
toJSON: function() {
var attrs = _.clone(this.attributes);
_.each(attrs, function(attrName, attrValue){
if(_.isDate(attrValue)){
attrs[attrName] = String(attrValue);
}
});
return attrs;
},
parse: function(attrs) {
//Parse dates strings into js objects
if(attrs._timestamp) {
attrs._timestamp = new Date(attrs._timestamp);
}
return attrs;
}
});
return Backbone.Collection.extend({
initialize: function(models, options){
this.options = options;
},
model: Tag,
saveToFS: function(options){
var that = this;
var filePath = sfsf.joinPaths(options.dirPath, that.options.id +
'.' + that.options.layerName + '.tags.json');
console.log("saving " + filePath);
sfsf.cretrieve(filePath, {
data: JSON.stringify(that.toJSON()),
type: 'text/plain'
}, function(error) {
if(error){
options.error(error);
return;
}
console.log("contents written!");
options.success();
});
}
});
});