@@ -47,6 +47,32 @@ angular.module('ngSails', []).factory('$sails', ['$q',
47
47
return this ;
48
48
} ;
49
49
50
+ $sails . prototype . crud = function ( method , object , id ) {
51
+ var q = $q . defer ( ) ;
52
+ object = object || { } ;
53
+ io . socket [ method ] ( this . prefix + this . model + ( id ? '/' + id : '' ) , object , function ( data , response ) {
54
+ var verb = false ;
55
+ switch ( method ) {
56
+ case "post" :
57
+ verb = "created" ;
58
+ break ;
59
+ case "put" :
60
+ verb = "updated" ;
61
+ break ;
62
+ case "delete" :
63
+ verb = "destroyed"
64
+ break ;
65
+ }
66
+ if ( verb ) this . responseHandler ( { verb :verb , id :data . id , data :data } ) ;
67
+ if ( response . status == 200 ) {
68
+ q . resolve ( data ) ;
69
+ } else {
70
+ q . reject ( data ) ;
71
+ }
72
+ } . bind ( this ) ) ;
73
+ return q . promise ;
74
+ }
75
+
50
76
$sails . prototype . hardFetch = function ( subscribe ) {
51
77
io . socket . request ( this . prefix + this . model , this . params , function ( response ) {
52
78
response . reverse ( ) ;
@@ -63,54 +89,19 @@ angular.module('ngSails', []).factory('$sails', ['$q',
63
89
} ;
64
90
65
91
$sails . prototype . create = function ( object ) {
66
- var q = $q . defer ( ) ;
67
- io . socket . post ( this . prefix + this . model , object , function ( data , response ) {
68
- this . responseHandler ( { verb :"created" , id :data . id , data :data } ) ;
69
- if ( response . status == 200 ) {
70
- q . resolve ( data ) ;
71
- } else {
72
- q . reject ( data ) ;
73
- }
74
- } . bind ( this ) ) ;
75
- return q . promise ;
92
+ return this . crud ( 'post' , object ) ;
76
93
} ;
77
94
78
95
$sails . prototype . update = function ( id , object ) {
79
- var q = $q . defer ( ) ;
80
- io . socket . put ( this . prefix + this . model + '/' + id , object , function ( data , response ) {
81
- this . responseHandler ( { verb :"updated" , id :id , data :response . data } ) ;
82
- if ( response . status == 200 ) {
83
- q . resolve ( data ) ;
84
- } else {
85
- q . reject ( data ) ;
86
- }
87
- } . bind ( this ) ) ;
88
- return q . promise ;
96
+ return this . crud ( 'put' , object , id ) ;
89
97
} ;
90
98
91
99
$sails . prototype . destroy = function ( id , object ) {
92
- var q = $q . defer ( ) ;
93
- io . socket . delete ( this . prefix + this . model + '/' + id , object , function ( data , response ) {
94
- this . responseHandler ( { verb :"destroyed" , id :id } ) ;
95
- if ( response . status == 200 ) {
96
- q . resolve ( data ) ;
97
- } else {
98
- q . reject ( data ) ;
99
- }
100
- } . bind ( this ) ) ;
101
- return q . promise ;
100
+ return this . crud ( 'delete' , object , id ) ;
102
101
} ;
103
102
104
103
$sails . prototype . retrieve = function ( id , object ) {
105
- var q = $q . defer ( ) ;
106
- io . socket . get ( this . prefix + this . model + '/' + id , object , function ( data , response ) {
107
- if ( response . status == 200 ) {
108
- q . resolve ( data ) ;
109
- } else {
110
- q . reject ( data ) ;
111
- }
112
- } . bind ( this ) ) ;
113
- return q . promise ;
104
+ return this . crud ( 'get' , object , id ) ;
114
105
} ;
115
106
return $sails ;
116
107
}
0 commit comments