emerson 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,4 +19,5 @@ tmp
19
19
  .gemset
20
20
  .rspec
21
21
  .rvmrc
22
- .import/
22
+ .import/
23
+ .sublime*
@@ -1,3 +1,3 @@
1
1
  module Emerson
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,4 +1,4 @@
1
- // Emerson.js 0.0.3
1
+ // Emerson.js 0.0.4
2
2
  //
3
3
  // (c) 2012 Corey Innis
4
4
  // Emerson may be freely distributed under the MIT license.
@@ -21,7 +21,7 @@
21
21
  }
22
22
 
23
23
  // Current version of the library. Keep in sync with `package.json`.
24
- Emerson.VERSION = '0.0.3';
24
+ Emerson.VERSION = '0.0.4';
25
25
 
26
26
  // Reference the base lib (one of jQuery, Zepto or Ender) as $.
27
27
  var $ = Emerson.base = (root.jQuery || root.Zepto || root.ender);
@@ -15,20 +15,7 @@
15
15
  // * `init` is a hook for initializing the module.
16
16
  _.extend(define, {
17
17
  ns : ns,
18
- init : function init() {
19
- _before.list = [];
20
- _after.list = [];
21
- },
22
- before : function before(callback) {
23
- if(callback) {
24
- _before(callback);
25
- }
26
- },
27
- after : function after(callback) {
28
- if(callback) {
29
- _after(callback);
30
- }
31
- }
18
+ init : function init() {}
32
19
  });
33
20
 
34
21
 
@@ -48,46 +35,11 @@
48
35
  var key = elem.data('sink');
49
36
 
50
37
  if(key) {
51
- _before.apply(elem);
52
38
  elem.replaceAll('[data-sink="' + key + '"]', 'body');
53
- _after.apply(elem);
39
+ elem.trigger('sink:after');
54
40
  }
55
41
  });
56
42
 
57
43
  return this;
58
44
  };
59
-
60
-
61
- // Internal Implementation
62
- // --------------------------------------------------------------------------
63
-
64
- // ### 'before' callbacks
65
- var _before = function(callback) {
66
- var view;
67
- if(callback) {
68
- _before.list.push(callback);
69
- }
70
- else {
71
- view = this;
72
- _.each(_before.list, function(fn) {
73
- fn(view);
74
- });
75
- }
76
- };
77
- _.extend(_before, { list : [] });
78
-
79
- // ### 'after' callbacks
80
- var _after = function(callback) {
81
- var view;
82
- if(callback) {
83
- _after.list.push(callback);
84
- }
85
- else {
86
- view = this;
87
- _.each(_after.list, function(fn) {
88
- fn(view);
89
- });
90
- }
91
- };
92
- _.extend(_after, { list : [] });
93
45
  })(Emerson);
@@ -9,14 +9,26 @@
9
9
 
10
10
  // ### Emerson.view module
11
11
  // Entry point for defining a new View.
12
- var define = ns.view = function(name, setup) {
13
- return (library[name] = construct(name, setup || {}));
12
+ var view = ns.view = function(name, setup) {
13
+ return (views[name] = construct(name, setup || {}));
14
+ };
15
+
16
+ // ### Emerson.trait module
17
+ // Entry point for defining a new "trait".
18
+ //
19
+ // A trait can be thought of as a set of related behaviors and is essentially
20
+ // a special form of view.
21
+ //
22
+ // Traits have the additional capability of responding to an optional "mode"
23
+ // at intialization.
24
+ var trait = ns.trait = function(name, setup) {
25
+ return (traits[name] = construct(name, setup || {}));
14
26
  };
15
27
 
16
28
  // ### Module API
17
29
  // * `ns` is a reference to the namespace.
18
30
  // * `init` is a hook for initializing the module.
19
- _.extend(define, {
31
+ _.extend(view, {
20
32
  ns : ns,
21
33
  init : function init() {
22
34
  configure();
@@ -36,21 +48,17 @@
36
48
  // $.view(key);
37
49
  //
38
50
  // Accessor for defined Views.
51
+ $.view = function(key) {
52
+ return views[key];
53
+ };
54
+
55
+ // ### $.trait
39
56
  //
40
- // Note that, while it is possible to manually execute View methods on an
41
- // object like so:
42
- //
43
- // $.view(key).fn.method.apply(object, arguments);
44
- //
45
- // such usage is not recommended as it:
46
- //
47
- // 1. circumvents the intentional transience provided by the framework.
48
- // 2. is likely to cause issues in that the called method will be working
49
- // with a non-initialized/-decorated object which may not have the
50
- // expected API.
57
+ // $.trait(key);
51
58
  //
52
- $.view = function(key) {
53
- return library[key];
59
+ // Accessor for defined Traits. See `$.view`.
60
+ $.trait = function(key) {
61
+ return traits[key];
54
62
  };
55
63
 
56
64
  // ### $.fn.view
@@ -72,14 +80,12 @@
72
80
 
73
81
  _.each(as_view, function(html) {
74
82
  var element = $(html);
75
- attach.apply(element, [element.data(attrs.view)]);
83
+ attach.call(element, views, [element.data(attrs.view)]);
76
84
  });
77
85
 
78
86
  _.each(as_trait, function(html) {
79
87
  var element = $(html);
80
- attach.apply(element, _.map(element.data(attrs.traits).split(/\s+/), function(key) {
81
- return [':', key].join('');
82
- }));
88
+ attach.call(element, traits, element.data(attrs.traits).split(/\s+/), true);
83
89
  });
84
90
  });
85
91
 
@@ -121,7 +127,11 @@
121
127
 
122
128
  // Storage place for the defined Views.
123
129
  // @private
124
- var library = {};
130
+ var views = {};
131
+
132
+ // Storage place for the defined Traits.
133
+ // @private
134
+ var traits = {};
125
135
 
126
136
  // Storage place for attachments made.
127
137
  // @private
@@ -166,8 +176,8 @@
166
176
  }
167
177
 
168
178
  // ### attach
169
- // Given a (complex) list of keys, search the library for applicable View and
170
- // Trait definitions to apply to the object.
179
+ // Given a (complex) list of keys, search the libraries for applicable View
180
+ // and Trait definitions to apply to the object.
171
181
  //
172
182
  // Keeps track of which definitions have been applied, and does not re-apply.
173
183
  //
@@ -175,15 +185,24 @@
175
185
  // a single element. e.g.,
176
186
  //
177
187
  // _.each($(selector), function(element) {
178
- // attach.apply($(element), [key, [subkey]]);
188
+ // attach.apply($(element), views, [key, [subkey]]);
179
189
  // });
180
- function attach() {
190
+ //
191
+ // The `mode_p` predicate argument indicates whether the view type may accept
192
+ // a "mode", which should be true for traits (not for views).
193
+ function attach(library, keys, mode_p) {
181
194
  var self = this, def;
182
195
  var id = eid(this[0]);
183
196
 
184
- _.each(_.flatten(arguments), function(key) {
185
- var set = (attachments[key] || (attachments[key] = []));
186
- var built, init, events;
197
+ _.each(_.flatten(keys), function(key) {
198
+ var mode, match, built, init, events, set;
199
+
200
+ if(mode_p && (match = /^([^(]+)\((.+)\)/.exec(key))) {
201
+ key = match[1];
202
+ mode = match[2];
203
+ }
204
+
205
+ set = (attachments[key] || (attachments[key] = []));
187
206
 
188
207
  if(_.include(set, id)) {
189
208
  return; // do not re-apply.
@@ -199,7 +218,7 @@
199
218
  bind(built, key, handler);
200
219
  });
201
220
 
202
- init.call(built);
221
+ init.call(built, mode);
203
222
  set.push(id);
204
223
  }
205
224
  });
@@ -220,10 +239,18 @@
220
239
  // },
221
240
  // document : { // bind document, for events
222
241
  // 'click' : handler // fired outside of the view
242
+ // 'selector' : { // TODO
243
+ // 'click' : handler
244
+ // }
223
245
  // }
224
246
  // }
225
247
  // });
226
248
  //
249
+ // Emerson event handling differs from that of, say, stock jQuery in that
250
+ // `this` within the context of the handler will be a view instance. The
251
+ // event argument is unadultered, allowing access to the full set of targets
252
+ // as defined by the baselib (e.g., jQuery).
253
+ //
227
254
  // Note that, in the document-binding case, an event like `click` would be a
228
255
  // bad idea. A more useful (and less costly) use case would be a form of
229
256
  // pub/sub.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emerson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-10 00:00:00.000000000 Z
12
+ date: 2012-05-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: transcendent views
15
15
  email: