Skip to content

Commit 5467830

Browse files
committed
Added function wrappings.
1 parent 4787f6c commit 5467830

File tree

4 files changed

+105
-13
lines changed

4 files changed

+105
-13
lines changed

Gruntfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ module.exports = function (grunt) {
8080
var banner = '/**\n' +
8181
'* @author Jason Dobry <[email protected]>\n' +
8282
'* @file js-data-angular.js\n' +
83-
'* @version ' + pkg.version + ' - Homepage <http://js-data-angular.pseudobry.com/>\n' +
83+
'* @version ' + pkg.version + ' - Homepage <http://www.js-data.io/js-data-angular/>\n' +
8484
'* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>\n' +
8585
'* @license MIT <https://github.com/js-data/js-data-angular/blob/master/LICENSE>\n' +
8686
'*\n' +
87-
'* @overview Data store for Angular.js.\n' +
87+
'* @overview Angular wrapper for js-data.js.\n' +
8888
'*/\n';
8989

9090
file = banner + file;

dist/js-data-angular.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/**
22
* @author Jason Dobry <[email protected]>
33
* @file js-data-angular.js
4-
* @version 2.0.0-alpha.1-0 - Homepage <http://js-data-angular.pseudobry.com/>
4+
* @version 2.0.0-alpha.1-0 - Homepage <http://www.js-data.io/js-data-angular/>
55
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
66
* @license MIT <https://github.com/js-data/js-data-angular/blob/master/LICENSE>
77
*
8-
* @overview Data store for Angular.js.
8+
* @overview Angular wrapper for js-data.js.
99
*/
1010
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
11+
/*jshint loopfunc:true*/
1112
(function (window, angular, undefined) {
1213
'use strict';
1314

@@ -16,6 +17,21 @@
1617
project: 'js-data-http',
1718
name: 'http',
1819
class: 'DSHttpAdapter'
20+
},
21+
{
22+
project: 'js-data-localstorage',
23+
name: 'localstorage',
24+
class: 'DSLocalStorageAdapter'
25+
},
26+
{
27+
project: 'js-data-localforage',
28+
name: 'localforage',
29+
class: 'DSLocalForageAdapter'
30+
},
31+
{
32+
project: 'js-data-firebase',
33+
name: 'firebase',
34+
class: 'DSFirebaseAdapter'
1935
}
2036
];
2137

@@ -34,7 +50,7 @@
3450

3551
if (Adapter) {
3652
adapter.loaded = true;
37-
angular.module(adapter.project, ['ng']).provider(adapter.class + 'Provider', function () {
53+
angular.module(adapter.project, ['ng']).provider(adapter.class, function () {
3854
var _this = this;
3955
_this.defaults = {};
4056
_this.$get = [function () {
@@ -64,6 +80,17 @@
6480
throw new Error('js-data must be loaded!');
6581
}
6682

83+
var functionsToWrap = [
84+
'compute',
85+
'digest',
86+
'eject',
87+
'inject',
88+
'link',
89+
'linkAll',
90+
'linkInverse',
91+
'unlinkInverse'
92+
];
93+
6794
angular.module('js-data', ['ng'])
6895
.factory('DSUtils', JSData.DSUtils)
6996
.factory('DSErrors', JSData.DSErrors)
@@ -80,17 +107,36 @@
80107

81108
_this.defaults = {};
82109

83-
deps.push(function () {
110+
function load() {
111+
var args = Array.prototype.slice.call(arguments);
112+
var $rootScope = args[args.length - 1];
84113
var store = new JSData.DS(_this.defaults);
114+
var originals = {};
85115

86116
for (var i = 0; i < adapters.length; i++) {
87117
if (adapters[i].loaded) {
88118
store.registerAdapter(adapters[i].name, arguments[i]);
89119
}
90120
}
91121

92-
return new JSData.DS(_this.defaults);
93-
});
122+
for (i = 0; i < functionsToWrap.length; i++) {
123+
originals[functionsToWrap[i]] = store[functionsToWrap[i]];
124+
store[functionsToWrap[i]] = function () {
125+
var args = arguments;
126+
if (!$rootScope.$$phase) {
127+
return $rootScope.$apply(function () {
128+
return originals[functionsToWrap[i]].apply(store, args);
129+
});
130+
}
131+
return originals[functionsToWrap[i]].apply(store, args);
132+
};
133+
}
134+
135+
return store;
136+
}
137+
138+
deps.push('$rootScope');
139+
deps.push(load);
94140

95141
_this.$get = deps;
96142
});

dist/js-data-angular.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*jshint loopfunc:true*/
12
(function (window, angular, undefined) {
23
'use strict';
34

@@ -6,6 +7,21 @@
67
project: 'js-data-http',
78
name: 'http',
89
class: 'DSHttpAdapter'
10+
},
11+
{
12+
project: 'js-data-localstorage',
13+
name: 'localstorage',
14+
class: 'DSLocalStorageAdapter'
15+
},
16+
{
17+
project: 'js-data-localforage',
18+
name: 'localforage',
19+
class: 'DSLocalForageAdapter'
20+
},
21+
{
22+
project: 'js-data-firebase',
23+
name: 'firebase',
24+
class: 'DSFirebaseAdapter'
925
}
1026
];
1127

@@ -24,7 +40,7 @@
2440

2541
if (Adapter) {
2642
adapter.loaded = true;
27-
angular.module(adapter.project, ['ng']).provider(adapter.class + 'Provider', function () {
43+
angular.module(adapter.project, ['ng']).provider(adapter.class, function () {
2844
var _this = this;
2945
_this.defaults = {};
3046
_this.$get = [function () {
@@ -54,6 +70,17 @@
5470
throw new Error('js-data must be loaded!');
5571
}
5672

73+
var functionsToWrap = [
74+
'compute',
75+
'digest',
76+
'eject',
77+
'inject',
78+
'link',
79+
'linkAll',
80+
'linkInverse',
81+
'unlinkInverse'
82+
];
83+
5784
angular.module('js-data', ['ng'])
5885
.factory('DSUtils', JSData.DSUtils)
5986
.factory('DSErrors', JSData.DSErrors)
@@ -70,17 +97,36 @@
7097

7198
_this.defaults = {};
7299

73-
deps.push(function () {
100+
function load() {
101+
var args = Array.prototype.slice.call(arguments);
102+
var $rootScope = args[args.length - 1];
74103
var store = new JSData.DS(_this.defaults);
104+
var originals = {};
75105

76106
for (var i = 0; i < adapters.length; i++) {
77107
if (adapters[i].loaded) {
78108
store.registerAdapter(adapters[i].name, arguments[i]);
79109
}
80110
}
81111

82-
return new JSData.DS(_this.defaults);
83-
});
112+
for (i = 0; i < functionsToWrap.length; i++) {
113+
originals[functionsToWrap[i]] = store[functionsToWrap[i]];
114+
store[functionsToWrap[i]] = function () {
115+
var args = arguments;
116+
if (!$rootScope.$$phase) {
117+
return $rootScope.$apply(function () {
118+
return originals[functionsToWrap[i]].apply(store, args);
119+
});
120+
}
121+
return originals[functionsToWrap[i]].apply(store, args);
122+
};
123+
}
124+
125+
return store;
126+
}
127+
128+
deps.push('$rootScope');
129+
deps.push(load);
84130

85131
_this.$get = deps;
86132
});

0 commit comments

Comments
 (0)