Skip to content

Commit 0d9b47d

Browse files
committed
0.1: add directive
1 parent 9b65435 commit 0d9b47d

4 files changed

Lines changed: 63 additions & 12 deletions

File tree

examples/example.js

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

33
app.controller('duoshuo', function($scope, $duoshuo){
4-
console.log($duoshuo);
4+
// inspect current user
5+
$duoshuo.on('ready', function(err, data) {
6+
console.log(data);
7+
$scope.responseJSON = JSON.stringify(data);
8+
$scope.$apply();
9+
});
510
});

examples/index.html

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,34 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Duoshuo Angular SDK</title>
6+
<style>
7+
h3 {
8+
font-size: 16px;
9+
}
10+
.angular-duoshuo {
11+
margin: 50px;
12+
}
13+
.response {
14+
font-size: 12px;
15+
color: #999;
16+
margin-bottom: 20px;
17+
}
18+
</style>
619
</head>
720
<body>
21+
<div id="angular-duoshuo" class="angular-duoshuo" ng-controller="duoshuo">
22+
<h3 ng-show="responseJSON">当前用户数据:</h3>
23+
<div class="response" ng-bind="responseJSON"></div>
24+
<div duoshuo thread-key="123"></div>
25+
</div>
826
<script>
927
var duoshuoQuery = {
10-
short_name: 'blabla'
28+
short_name: '111'
1129
}
1230
</script>
13-
<script src="../bower_components/angular/angular.min.js"></script>
1431
<script src="http://static.duoshuo.com/embed.unstable.js"></script>
32+
<script src="../bower_components/angular/angular.min.js"></script>
1533
<script src="../src/duoshuo.js"></script>
1634
<script src="./example.js"></script>
1735
</body>
18-
</html>
36+
</html>

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"description": "a duoshuo SDK for angular.js, pure front-end, cross-domain request supported.",
55
"main": "src/duoshuo.js",
66
"scripts": {
7-
"serve": "./node_modules/.bin/serve ./examples",
8-
"start": "npm run dev",
7+
"serve": "./node_modules/.bin/serve .",
8+
"dev": "npm run serve",
9+
"start": "npm run serve",
910
"example": "npm run serve"
1011
},
1112
"repository": {

src/duoshuo.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
;(function(angular, duoshuo, API, configs) {
1+
;
2+
(function(angular, duoshuo, API, configs) {
23

34
'use strict';
45

@@ -11,7 +12,33 @@
1112
angular.module('duoshuo', [])
1213
.factory('$duoshuo', function() {
1314
return new Duoshuo(configs);
14-
});
15+
})
16+
.directive('duoshuo', ['$duoshuo',
17+
function($duoshuo) {
18+
return {
19+
restrict: 'EA',
20+
replace: true,
21+
scope: {
22+
threadKey: '=',
23+
title: '=',
24+
image: '=',
25+
url: '=',
26+
authorKey: '=',
27+
formPosition: '=',
28+
limit: '=',
29+
order: '='
30+
},
31+
template: '<div class="ds-thread"></div>',
32+
link: function(scope, element, attrs) {
33+
scope.$watch('threadKey', function(threadKey) {
34+
if (angular.isDefined(threadKey)) {
35+
// $duoshuo.loadComment(scope, element[0]);
36+
}
37+
});
38+
}
39+
};
40+
}
41+
]);
1542

1643
function Duoshuo() {
1744
this.configs = configs;
@@ -28,16 +55,16 @@
2855
if (this.events.indexOf(eve) === 0) return callback(new Error('event not found'));
2956
var e = eve;
3057
if (e === 'ready') e = 'reset';
31-
return DUOSHUO.visitor.on(e, function(){
58+
return DUOSHUO.visitor.on(e, function() {
3259
var self = this;
3360
var data = this.data;
3461
return callback(null, data, self);
3562
});
3663
};
3764

3865
})(
39-
window.angular,
40-
window.DUOSHUO,
66+
window.angular,
67+
window.DUOSHUO,
4168
window.DUOSHUO.API,
4269
window.duoshuoQuery
4370
);

0 commit comments

Comments
 (0)