-
Notifications
You must be signed in to change notification settings - Fork 4
/
backbone-sync-github.js
121 lines (93 loc) · 3.76 KB
/
backbone-sync-github.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* User: k33g
* Date: 26/08/11
*/
var Store = function(user,repo,branch,directory, callBk) {
var that = this;
this.parameters = {
gitHubBranchId : "",
gitHubBranchName : branch,
gitHubUser : user,
gitHubRepo : repo,
gitHubDir : directory
};
gh.repo(this.parameters.gitHubUser, this.parameters.gitHubRepo).branches(function(data){
//get Master Branch sha
that.parameters.gitHubBranchId = data.branches[that.parameters.gitHubBranchName];
callBk();
});
};
_.extend(Store.prototype, {
save: function() {
throw "save function not implemented";
},
create: function() {
throw "create function not implemented";
},
update: function() {
throw "update function not implemented";
},
find: function(model, callBk) {
var that = this;
this.callBk = callBk;
gh.object(this.parameters.gitHubUser, this.parameters.gitHubRepo)
.blob(this.parameters.gitHubDir +"/" + model.id ,this.parameters.gitHubBranchId,function(result){
gh.commit.forPath(that.parameters.gitHubUser, that.parameters.gitHubRepo,
that.parameters.gitHubBranchName, result.blob.name ,function(inf){
var lastCommit = inf.commits[0];
var firstCommit = inf.commits[inf.commits.length-1];
model.set({author : lastCommit.author.login});
model.set({dateCreate : firstCommit.committed_date});
model.set({dateUpdate : lastCommit.committed_date});
model.set({message : lastCommit.message});
model.set({text : result.blob.data});
that.callBk.result(model);
});
});
},
// Return the array of all models
findAll: function(model,callBk) { /* model is a collection */
var that = this;
this.callBkFindAll = callBk;
this.postsIndexes = [];
gh.object(this.parameters.gitHubUser, this.parameters.gitHubRepo).tree(this.parameters.gitHubBranchId,function(contents){
var tree = contents.tree.filter(function(e) { return e.name === that.parameters.gitHubDir; })[0];
gh.object(that.parameters.gitHubUser, that.parameters.gitHubRepo).tree(tree.sha,function(contents){
var Files = contents.tree;
Files.reverse().forEach(function(f){
that.postsIndexes.push(f);
});
var count = that.postsIndexes.length, i=0;
that.postsIndexes.forEach(function(element){
var post = new Post({ id : element.name });
model.add(post);
model.get(element.name).fetch({ result : function(){
i+=1;
if(i==count) {
that.callBkFindAll.result();
}
}
});
});
});
});
},
destroy: function() {
throw "destroy function not implemented";
}
});
Backbone.sync = function(method, model, options) {
var resp;
var store = model.gitHubStorage || model.collection.gitHubStorage;
switch (method) {
case "read": resp = model.id ? store.find(model, options) : store.findAll(model, options); break;
case "create": resp = store.create(model); break;
case "update": resp = store.update(model); break;
case "delete": resp = store.destroy(model); break;
}
if (resp) {
options.success(resp);
} else {
options.error("Record not found");
}
};