amplify 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Amplify
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -15,6 +15,7 @@ var slice = [].slice,
15
15
  var amplify = global.amplify = {
16
16
  publish: function( topic ) {
17
17
  var args = slice.call( arguments, 1 ),
18
+ topicSubscriptions,
18
19
  subscription,
19
20
  length,
20
21
  i = 0,
@@ -24,8 +25,9 @@ var amplify = global.amplify = {
24
25
  return true;
25
26
  }
26
27
 
27
- for ( length = subscriptions[ topic ].length; i < length; i++ ) {
28
- subscription = subscriptions[ topic ][ i ];
28
+ topicSubscriptions = subscriptions[ topic ].slice();
29
+ for ( length = topicSubscriptions.length; i < length; i++ ) {
30
+ subscription = topicSubscriptions[ i ];
29
31
  ret = subscription.callback.apply( subscription.context, args );
30
32
  if ( ret === false ) {
31
33
  break;
@@ -48,9 +50,11 @@ var amplify = global.amplify = {
48
50
 
49
51
  var topicIndex = 0,
50
52
  topics = topic.split( /\s/ ),
51
- topicLength = topics.length;
53
+ topicLength = topics.length,
54
+ added;
52
55
  for ( ; topicIndex < topicLength; topicIndex++ ) {
53
56
  topic = topics[ topicIndex ];
57
+ added = false;
54
58
  if ( !subscriptions[ topic ] ) {
55
59
  subscriptions[ topic ] = [];
56
60
  }
@@ -65,11 +69,14 @@ var amplify = global.amplify = {
65
69
  for ( ; i >= 0; i-- ) {
66
70
  if ( subscriptions[ topic ][ i ].priority <= priority ) {
67
71
  subscriptions[ topic ].splice( i + 1, 0, subscriptionInfo );
68
- return callback;
72
+ added = true;
73
+ break;
69
74
  }
70
75
  }
71
-
72
- subscriptions[ topic ].unshift( subscriptionInfo );
76
+
77
+ if ( !added ) {
78
+ subscriptions[ topic ].unshift( subscriptionInfo );
79
+ }
73
80
  }
74
81
 
75
82
  return callback;
@@ -93,4 +100,3 @@ var amplify = global.amplify = {
93
100
  };
94
101
 
95
102
  }( this ) );
96
-
@@ -15,6 +15,24 @@ function isFunction( obj ) {
15
15
  return ({}).toString.call( obj ) === "[object Function]";
16
16
  }
17
17
 
18
+ function async( fn ) {
19
+ var isAsync = false;
20
+ setTimeout(function() {
21
+ isAsync = true;
22
+ }, 1 );
23
+ return function() {
24
+ var that = this,
25
+ args = arguments;
26
+ if ( isAsync ) {
27
+ fn.apply( that, args );
28
+ } else {
29
+ setTimeout(function() {
30
+ fn.apply( that, args );
31
+ }, 1 );
32
+ }
33
+ };
34
+ }
35
+
18
36
  amplify.request = function( resourceId, data, callback ) {
19
37
  // default to an empty hash just so we can handle a missing resourceId
20
38
  // in one place
@@ -36,18 +54,18 @@ amplify.request = function( resourceId, data, callback ) {
36
54
  resource = amplify.request.resources[ settings.resourceId ],
37
55
  success = settings.success || noop,
38
56
  error = settings.error || noop;
39
- settings.success = function( data, status ) {
57
+ settings.success = async( function( data, status ) {
40
58
  status = status || "success";
41
59
  amplify.publish( "request.success", settings, data, status );
42
60
  amplify.publish( "request.complete", settings, data, status );
43
61
  success( data, status );
44
- };
45
- settings.error = function( data, status ) {
62
+ });
63
+ settings.error = async( function( data, status ) {
46
64
  status = status || "error";
47
65
  amplify.publish( "request.error", settings, data, status );
48
66
  amplify.publish( "request.complete", settings, data, status );
49
67
  error( data, status );
50
- };
68
+ });
51
69
 
52
70
  if ( !resource ) {
53
71
  if ( !settings.resourceId ) {
@@ -101,10 +119,8 @@ amplify.request.types.ajax = function( defnSettings ) {
101
119
  return function( settings, request ) {
102
120
  var xhr,
103
121
  url = defnSettings.url,
104
- data = settings.data,
105
122
  abort = request.abort,
106
- ajaxSettings = {},
107
- mappedKeys = [],
123
+ ajaxSettings = $.extend( true, {}, defnSettings, { data: settings.data } ),
108
124
  aborted = false,
109
125
  ampXHR = {
110
126
  readyState: 0,
@@ -136,27 +152,10 @@ amplify.request.types.ajax = function( defnSettings ) {
136
152
  }
137
153
  };
138
154
 
139
- if ( typeof data !== "string" ) {
140
- data = $.extend( true, {}, defnSettings.data, data );
141
-
142
- url = url.replace( rurlData, function ( m, key ) {
143
- if ( key in data ) {
144
- mappedKeys.push( key );
145
- return data[ key ];
146
- }
147
- });
148
-
149
- // We delete the keys later so duplicates are still replaced
150
- $.each( mappedKeys, function ( i, key ) {
151
- delete data[ key ];
152
- });
153
- }
155
+ amplify.publish( "request.ajax.preprocess",
156
+ defnSettings, settings, ajaxSettings, ampXHR );
154
157
 
155
- $.extend( ajaxSettings, defnSettings, {
156
- url: url,
157
- type: defnSettings.type,
158
- data: data,
159
- dataType: defnSettings.dataType,
158
+ $.extend( ajaxSettings, {
160
159
  success: function( data, status ) {
161
160
  handleResponse( data, status );
162
161
  },
@@ -212,6 +211,56 @@ amplify.request.types.ajax = function( defnSettings ) {
212
211
 
213
212
 
214
213
 
214
+ amplify.subscribe( "request.ajax.preprocess", function( defnSettings, settings, ajaxSettings ) {
215
+ var mappedKeys = [],
216
+ data = ajaxSettings.data;
217
+
218
+ if ( typeof data === "string" ) {
219
+ return;
220
+ }
221
+
222
+ data = $.extend( true, {}, defnSettings.data, data );
223
+
224
+ ajaxSettings.url = ajaxSettings.url.replace( rurlData, function ( m, key ) {
225
+ if ( key in data ) {
226
+ mappedKeys.push( key );
227
+ return data[ key ];
228
+ }
229
+ });
230
+
231
+ // We delete the keys later so duplicates are still replaced
232
+ $.each( mappedKeys, function ( i, key ) {
233
+ delete data[ key ];
234
+ });
235
+
236
+ ajaxSettings.data = data;
237
+ });
238
+
239
+
240
+
241
+ amplify.subscribe( "request.ajax.preprocess", function( defnSettings, settings, ajaxSettings ) {
242
+ var data = ajaxSettings.data,
243
+ dataMap = defnSettings.dataMap;
244
+
245
+ if ( !dataMap || typeof data === "string" ) {
246
+ return;
247
+ }
248
+
249
+ if ( $.isFunction( dataMap ) ) {
250
+ ajaxSettings.data = dataMap( data );
251
+ } else {
252
+ $.each( defnSettings.dataMap, function( orig, replace ) {
253
+ if ( orig in data ) {
254
+ data[ replace ] = data[ orig ];
255
+ delete data[ orig ];
256
+ }
257
+ });
258
+ ajaxSettings.data = data;
259
+ }
260
+ });
261
+
262
+
263
+
215
264
  var cache = amplify.request.cache = {
216
265
  _key: function( resourceId, url, data ) {
217
266
  data = url + data;
@@ -334,4 +383,3 @@ amplify.subscribe( "request.before.ajax", function( resource, settings, ajaxSett
334
383
  });
335
384
 
336
385
  }( amplify, jQuery ) );
337
-
@@ -123,7 +123,7 @@ for ( var webStorageType in { localStorage: 1, sessionStorage: 1 } ) {
123
123
  // globalStorage
124
124
  // non-standard: Firefox 2+
125
125
  // https://developer.mozilla.org/en/dom/storage#globalStorage
126
- if ( window.globalStorage ) {
126
+ if ( !store.types.localStorage && window.globalStorage ) {
127
127
  // try/catch for file protocol in Firefox
128
128
  try {
129
129
  createFromStorageInterface( "globalStorage",
@@ -153,65 +153,88 @@ if ( window.globalStorage ) {
153
153
  attrKey = "amplify";
154
154
  div.style.display = "none";
155
155
  document.getElementsByTagName( "head" )[ 0 ].appendChild( div );
156
- if ( div.addBehavior ) {
156
+
157
+ // we can't feature detect userData support
158
+ // so just try and see if it fails
159
+ // surprisingly, even just adding the behavior isn't enough for a failure
160
+ // so we need to load the data as well
161
+ try {
157
162
  div.addBehavior( "#default#userdata" );
163
+ div.load( attrKey );
164
+ } catch( e ) {
165
+ div.parentNode.removeChild( div );
166
+ return;
167
+ }
158
168
 
159
- store.addType( "userData", function( key, value, options ) {
160
- div.load( attrKey );
161
- var attr, parsed, prevValue, i, remove,
162
- ret = value,
163
- now = (new Date()).getTime();
169
+ store.addType( "userData", function( key, value, options ) {
170
+ div.load( attrKey );
171
+ var attr, parsed, prevValue, i, remove,
172
+ ret = value,
173
+ now = (new Date()).getTime();
164
174
 
165
- if ( !key ) {
166
- ret = {};
167
- remove = [];
168
- i = 0;
169
- while ( attr = div.XMLDocument.documentElement.attributes[ i++ ] ) {
170
- parsed = JSON.parse( attr.value );
171
- if ( parsed.expires && parsed.expires <= now ) {
172
- remove.push( attr.name );
173
- } else {
174
- ret[ attr.name ] = parsed.data;
175
- }
176
- }
177
- while ( key = remove.pop() ) {
178
- div.removeAttribute( key );
175
+ if ( !key ) {
176
+ ret = {};
177
+ remove = [];
178
+ i = 0;
179
+ while ( attr = div.XMLDocument.documentElement.attributes[ i++ ] ) {
180
+ parsed = JSON.parse( attr.value );
181
+ if ( parsed.expires && parsed.expires <= now ) {
182
+ remove.push( attr.name );
183
+ } else {
184
+ ret[ attr.name ] = parsed.data;
179
185
  }
180
- div.save( attrKey );
181
- return ret;
182
186
  }
187
+ while ( key = remove.pop() ) {
188
+ div.removeAttribute( key );
189
+ }
190
+ div.save( attrKey );
191
+ return ret;
192
+ }
183
193
 
184
- // convert invalid characters to dashes
185
- // http://www.w3.org/TR/REC-xml/#NT-Name
186
- // simplified to assume the starting character is valid
187
- // also removed colon as it is invalid in HTML attribute names
188
- key = key.replace( /[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g, "-" );
194
+ // convert invalid characters to dashes
195
+ // http://www.w3.org/TR/REC-xml/#NT-Name
196
+ // simplified to assume the starting character is valid
197
+ // also removed colon as it is invalid in HTML attribute names
198
+ key = key.replace( /[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g, "-" );
189
199
 
190
- if ( value === undefined ) {
191
- attr = div.getAttribute( key );
192
- parsed = attr ? JSON.parse( attr ) : { expires: -1 };
193
- if ( parsed.expires && parsed.expires <= now ) {
194
- div.removeAttribute( key );
195
- } else {
196
- return parsed.data;
197
- }
200
+ if ( value === undefined ) {
201
+ attr = div.getAttribute( key );
202
+ parsed = attr ? JSON.parse( attr ) : { expires: -1 };
203
+ if ( parsed.expires && parsed.expires <= now ) {
204
+ div.removeAttribute( key );
198
205
  } else {
199
- if ( value === null ) {
200
- div.removeAttribute( key );
201
- } else {
202
- // we need to get the previous value in case we need to rollback
203
- prevValue = div.getAttribute( key );
204
- parsed = JSON.stringify({
205
- data: value,
206
- expires: (options.expires ? (now + options.expires) : null)
207
- });
208
- div.setAttribute( key, parsed );
209
- }
206
+ return parsed.data;
207
+ }
208
+ } else {
209
+ if ( value === null ) {
210
+ div.removeAttribute( key );
211
+ } else {
212
+ // we need to get the previous value in case we need to rollback
213
+ prevValue = div.getAttribute( key );
214
+ parsed = JSON.stringify({
215
+ data: value,
216
+ expires: (options.expires ? (now + options.expires) : null)
217
+ });
218
+ div.setAttribute( key, parsed );
219
+ }
220
+ }
221
+
222
+ try {
223
+ div.save( attrKey );
224
+ // quota exceeded
225
+ } catch ( error ) {
226
+ // roll the value back to the previous value
227
+ if ( prevValue === null ) {
228
+ div.removeAttribute( key );
229
+ } else {
230
+ div.setAttribute( key, prevValue );
210
231
  }
211
232
 
233
+ // expire old data and try again
234
+ store.userData();
212
235
  try {
236
+ div.setAttribute( key, parsed );
213
237
  div.save( attrKey );
214
- // quota exceeded
215
238
  } catch ( error ) {
216
239
  // roll the value back to the previous value
217
240
  if ( prevValue === null ) {
@@ -219,31 +242,18 @@ if ( window.globalStorage ) {
219
242
  } else {
220
243
  div.setAttribute( key, prevValue );
221
244
  }
222
-
223
- // expire old data and try again
224
- store.userData();
225
- try {
226
- div.setAttribute( key, parsed );
227
- div.save( attrKey );
228
- } catch ( error ) {
229
- // roll the value back to the previous value
230
- if ( prevValue === null ) {
231
- div.removeAttribute( key );
232
- } else {
233
- div.setAttribute( key, prevValue );
234
- }
235
- throw store.error();
236
- }
245
+ throw store.error();
237
246
  }
238
- return ret;
239
- });
240
- }
247
+ }
248
+ return ret;
249
+ });
241
250
  }() );
242
251
 
243
252
  // in-memory storage
244
253
  // fallback for all browsers to enable the API even if we can't persist data
245
254
  (function() {
246
- var memory = {};
255
+ var memory = {},
256
+ timeout = {};
247
257
 
248
258
  function copy( obj ) {
249
259
  return obj === undefined ? undefined : JSON.parse( JSON.stringify( obj ) );
@@ -258,6 +268,11 @@ if ( window.globalStorage ) {
258
268
  return copy( memory[ key ] );
259
269
  }
260
270
 
271
+ if ( timeout[ key ] ) {
272
+ clearTimeout( timeout[ key ] );
273
+ delete timeout[ key ];
274
+ }
275
+
261
276
  if ( value === null ) {
262
277
  delete memory[ key ];
263
278
  return null;
@@ -265,8 +280,9 @@ if ( window.globalStorage ) {
265
280
 
266
281
  memory[ key ] = value;
267
282
  if ( options.expires ) {
268
- setTimeout(function() {
283
+ timeout[ key ] = setTimeout(function() {
269
284
  delete memory[ key ];
285
+ delete timeout[ key ];
270
286
  }, options.expires );
271
287
  }
272
288
 
@@ -275,4 +291,3 @@ if ( window.globalStorage ) {
275
291
  }() );
276
292
 
277
293
  }( this.amplify = this.amplify || {} ) );
278
-
metadata CHANGED
@@ -1,16 +1,11 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: amplify
3
- version: !ruby/object:Gem::Version
4
- hash: 21
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 0
9
- - 1
10
- version: 1.0.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
13
- - "Scott Gonz\xC3\xA1lez"
7
+ authors:
8
+ - Scott González
14
9
  - Andrew Wirick
15
10
  - Jonathan Sharp
16
11
  - Douglas Neiner
@@ -20,94 +15,68 @@ authors:
20
15
  autorequire:
21
16
  bindir: bin
22
17
  cert_chain: []
23
-
24
- date: 2011-06-29 00:00:00 -05:00
25
- default_executable:
26
- dependencies:
27
- - !ruby/object:Gem::Dependency
18
+ date: 2011-12-06 00:00:00.000000000Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
28
21
  name: jquery-rails
29
- prerelease: false
30
- requirement: &id001 !ruby/object:Gem::Requirement
22
+ requirement: &70335421748260 !ruby/object:Gem::Requirement
31
23
  none: false
32
- requirements:
24
+ requirements:
33
25
  - - ~>
34
- - !ruby/object:Gem::Version
35
- hash: 15
36
- segments:
37
- - 1
38
- - 0
39
- - 12
40
- version: 1.0.12
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
41
28
  type: :runtime
42
- version_requirements: *id001
43
- - !ruby/object:Gem::Dependency
44
- name: sprockets
45
29
  prerelease: false
46
- requirement: &id002 !ruby/object:Gem::Requirement
30
+ version_requirements: *70335421748260
31
+ - !ruby/object:Gem::Dependency
32
+ name: sprockets
33
+ requirement: &70335421747780 !ruby/object:Gem::Requirement
47
34
  none: false
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- hash: 62196471
52
- segments:
53
- - 2
54
- - 0
55
- - 0
56
- - beta
57
- - 10
58
- version: 2.0.0.beta.10
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '2.1'
59
39
  type: :runtime
60
- version_requirements: *id002
61
- description: AmplifyJS is a set of components designed to solve common web application problems with a simplistic API.
40
+ prerelease: false
41
+ version_requirements: *70335421747780
42
+ description: AmplifyJS is a set of components designed to solve common web application
43
+ problems with a simplistic API.
62
44
  email: alassek@lyconic.com
63
45
  executables: []
64
-
65
46
  extensions: []
66
-
67
47
  extra_rdoc_files: []
68
-
69
- files:
48
+ files:
70
49
  - lib/amplify/engine.rb
71
50
  - lib/amplify/version.rb
72
51
  - lib/amplify.rb
73
- - app/assets/javascripts/amplify/core.js.erb
74
- - app/assets/javascripts/amplify/index.js.erb
75
- - app/assets/javascripts/amplify/request.js.erb
76
- - app/assets/javascripts/amplify/store.js.erb
77
- has_rdoc: true
52
+ - lib/assets/javascripts/amplify/core.js.erb
53
+ - lib/assets/javascripts/amplify/index.js.erb
54
+ - lib/assets/javascripts/amplify/request.js.erb
55
+ - lib/assets/javascripts/amplify/store.js.erb
78
56
  homepage: http://amplifyjs.com
79
- licenses:
57
+ licenses:
80
58
  - MIT
81
59
  - GPL-2
82
60
  post_install_message:
83
61
  rdoc_options: []
84
-
85
- require_paths:
62
+ require_paths:
86
63
  - lib
87
- required_ruby_version: !ruby/object:Gem::Requirement
64
+ required_ruby_version: !ruby/object:Gem::Requirement
88
65
  none: false
89
- requirements:
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- hash: 3
93
- segments:
94
- - 0
95
- version: "0"
96
- required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
71
  none: false
98
- requirements:
99
- - - ">="
100
- - !ruby/object:Gem::Version
101
- hash: 3
102
- segments:
103
- - 0
104
- version: "0"
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
105
76
  requirements: []
106
-
107
77
  rubyforge_project:
108
- rubygems_version: 1.3.7
78
+ rubygems_version: 1.8.8
109
79
  signing_key:
110
80
  specification_version: 3
111
81
  summary: A Javascript Component Library
112
82
  test_files: []
113
-