|
9 | 9 | if (!configs.short_name) throw new Error('duoshuo short_name required!'); |
10 | 10 |
|
11 | 11 | 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; |
20 | 14 |
|
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 | + } |
35 | 24 | }); |
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 | + }); |
37 | 41 |
|
38 | 42 | })( |
39 | 43 | window.angular, |
|
0 commit comments