Skip to content

Commit e205dea

Browse files
committed
0.1: support auto $apply, bugfix
ready for shipping
1 parent 77d4ef6 commit e205dea

6 files changed

Lines changed: 46 additions & 31 deletions

File tree

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ node_modules
44
psd
55
thumb
66
*.log
7-
bower_*
7+
bower_*
8+
examples

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ See more examples in `./examples`, or just run `$ npm run example`
2222
angular.module('myApp',['duoshuo'])
2323
.controller('myController', function($scope, $duoshuo){
2424
// high level api
25-
$duoshuo.threads.create({
25+
$duoshuo.post('threads/create', {
2626
title: 'new thread',
2727
content: 'blablablabla'
28-
}, function(err, response, data){
29-
if (err) return console.error(err);
28+
}, function(data){
3029
console.log(data);
3130
});
3231
});

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"node_modules",
2121
"bower_components",
2222
"test",
23-
"tests"
23+
"tests",
24+
"examples"
2425
],
2526
"dependencies": {
2627
"angular": "*"

examples/example.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
var app = angular.module('app', ['duoshuo']);
22

3-
app.controller('duoshuo', function($scope, $duoshuo){
3+
app.controller('duoshuo', function($scope, $duoshuo) {
4+
// console.log($duoshuo);
45
// inspect current user
56
$duoshuo.on('ready', function(err, data) {
67
console.log(data);
78
$scope.responseJSON = JSON.stringify(data);
8-
$scope.$apply();
99
});
10-
});
10+
// using lowlevel `get` method
11+
$duoshuo.get('threads/list', {
12+
page: 1,
13+
limit: 30
14+
}, function(data) {
15+
$scope.threads = data.response;
16+
});
17+
});

examples/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<div id="angular-duoshuo" class="angular-duoshuo" ng-controller="duoshuo">
2222
<h3 ng-show="responseJSON">当前用户数据:</h3>
2323
<div class="response" ng-bind="responseJSON"></div>
24+
<div class="threads" ng-repeat="thread in threads">
25+
<h3 ng-bind="thread.title" ng-show="thread.title !== ''"></h3>
26+
</div>
2427
</div>
2528
<script>
2629
var duoshuoQuery = {

src/duoshuo.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,35 @@
99
if (!configs.short_name) throw new Error('duoshuo short_name required!');
1010

1111
angular.module('duoshuo', [])
12-
.factory('$duoshuo', function() {
13-
return new Duoshuo(configs);
14-
});
15-
16-
function Duoshuo() {
17-
this.configs = configs;
18-
this.events = ['reset', 'ready'];
19-
}
12+
.service('$duoshuo', function($rootScope) {
13+
var self = this;
2014

21-
// lowlevel api set
22-
Duoshuo.prototype.get = API.get;
23-
Duoshuo.prototype.post = API.post;
24-
Duoshuo.prototype.ajax = API.ajax;
25-
26-
// events
27-
Duoshuo.prototype.on = function(eve, callback) {
28-
if (this.events.indexOf(eve) === 0) return callback(new Error('event not found'));
29-
var e = eve;
30-
if (e === 'ready') e = 'reset';
31-
return DUOSHUO.visitor.on(e, function() {
32-
var self = this;
33-
var data = this.data;
34-
return callback(null, data, self);
15+
// lowlevel api set
16+
angular.forEach(['get', 'post', 'ajax'], function(method) {
17+
self[method] = function(endpoint, data, callback, skipCheck) {
18+
return API[method](endpoint, data, function(d) {
19+
callback(d);
20+
if (!skipCheck) $rootScope.$apply();
21+
return;
22+
});
23+
}
3524
});
36-
};
25+
26+
// event wrapper
27+
this.on = function(eve, callback, skipCheck) {
28+
if (['reset', 'ready'].indexOf(eve) === 0)
29+
return callback(new Error('event not found'));
30+
var e = eve;
31+
if (e === 'ready') e = 'reset';
32+
return DUOSHUO.visitor.on(e, function() {
33+
var self = this;
34+
var data = this.data;
35+
callback(null, data, self);
36+
if (!skipCheck) $rootScope.$apply();
37+
return;
38+
});
39+
};
40+
});
3741

3842
})(
3943
window.angular,

0 commit comments

Comments
 (0)