emerson 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Emerson
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1,4 +1,4 @@
1
- // Emerson.js 0.0.7
1
+ // Emerson.js 0.0.8
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.7';
24
+ Emerson.VERSION = '0.0.8';
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);
@@ -45,32 +45,31 @@
45
45
  // Pass an `override` argument to target a specific sink, say:
46
46
  //
47
47
  // data-sink="modal"
48
+ //
49
+ // Strategies:
50
+ //
51
+ // * `replace` (default)
52
+ // * `inner`
53
+ // * `prepend`
54
+ // * `append`
55
+ // * `before`
56
+ // * `after`
57
+ //
58
+ // data-sink"modal:inner"
59
+ //
48
60
  $.fn.sink = function(override) {
49
61
  _.each(this, function(e) {
50
- var elem = $(e);
51
- var key = override || elem.data('sink');
52
- var selector, matches;
62
+ var element = $(e);
63
+ var key = override || element.data('sink');
64
+ var selector, matches, count;
53
65
 
54
66
  if(key) {
55
- selector = '[data-sink="' + key + '"]';
67
+ selector = '[data-sink^="' + key + '"]';
56
68
  matches = $(selector);
69
+ count = matches.length;
57
70
 
58
- if(matches.length > 1) {
59
- matches.each(function() {
60
- var replacement = prepare(elem.clone(true));
61
-
62
- $(this)
63
- .trigger('sink:before')
64
- .replaceWith(replacement);
65
-
66
- replacement.trigger('sink:after');
67
- });
68
- }
69
- else {
70
- matches.trigger('sink:before');
71
- prepare(elem)
72
- .replaceAll(matches)
73
- .trigger('sink:after');
71
+ if(count) {
72
+ process(element, matches);
74
73
  }
75
74
  }
76
75
  });
@@ -82,6 +81,18 @@
82
81
  // Internal Implementation
83
82
  // --------------------------------------------------------------------------
84
83
 
84
+ var strategies = {
85
+ replace : $.fn.replaceAll,
86
+ inner : function(target) {
87
+ target.empty();
88
+ return this.appendTo(target);
89
+ },
90
+ prepend : $.fn.prependTo,
91
+ append : $.fn.appendTo,
92
+ before : $.fn.insertBefore,
93
+ after : $.fn.insertAfter
94
+ };
95
+
85
96
  // ### prepare
86
97
  // Clone the replacement source and, if Emerson.view is defined, apply that.
87
98
  function prepare(source) {
@@ -93,4 +104,32 @@
93
104
 
94
105
  return result;
95
106
  }
107
+
108
+ // ### process
109
+ // ...
110
+ function process(element, matches) {
111
+ var parts, strategy, clone;
112
+
113
+ if(matches.length > 1) {
114
+ matches.each(function() {
115
+ clone = prepare(element.clone(true));
116
+ parts = $(this).data('sink').split(':');
117
+ strategy = parts[1] || 'replace';
118
+
119
+ $(this).trigger('sink:before');
120
+ strategies[strategy].call(clone, $(this))
121
+ .trigger('sink:after');
122
+
123
+ clone.trigger('sink:after');
124
+ });
125
+ }
126
+ else {
127
+ parts = matches.data('sink').split(':');
128
+ strategy = parts[1] || 'replace';
129
+
130
+ matches.trigger('sink:before');
131
+ strategies[strategy].call(prepare(element), matches)
132
+ .trigger('sink:after');
133
+ }
134
+ }
96
135
  })(Emerson);
@@ -213,8 +213,16 @@
213
213
  // 2. A "subclass", extended with the View prototype.
214
214
  function construct(name, setup) {
215
215
  var sub = _.extend($sub(), {
216
- constructor : View,
217
- setup : _.extend({}, View.setup, setup)
216
+ constructor : View,
217
+ setup : _.extend({
218
+ initialize : function() {},
219
+ connected : false,
220
+ subscribe : {},
221
+ }, setup),
222
+ // experimental... allows for easier spec'ing of a view/trait
223
+ build : function build(element, mode) {
224
+ return instance(this, element, mode);
225
+ }
218
226
  });
219
227
 
220
228
  _.extend(sub.fn, View.prototype);
@@ -222,6 +230,21 @@
222
230
  return sub;
223
231
  }
224
232
 
233
+ // ### instance
234
+ // experimental (see `build` definition in `construct`).
235
+ function instance(def, element, mode) {
236
+ var setup = def.setup;
237
+ var built = def(element, element.context);
238
+ built.connect(setup.connected);
239
+
240
+ _.each(setup.subscribe, function(handler, key) {
241
+ bind(built, key, handler);
242
+ });
243
+
244
+ setup.initialize.call(built, mode);
245
+ return built;
246
+ }
247
+
225
248
  // ### attach
226
249
  // Given a (complex) list of keys, search the libraries for applicable View
227
250
  // and Trait definitions to apply to the object.
@@ -257,15 +280,17 @@
257
280
 
258
281
  // Build an instance, connect outlets, bind events, init and record.
259
282
  if(def = library[key]) {
260
- setup = def.setup;
261
- built = def(self, self.context);
262
- built.connect(setup.connected);
263
-
264
- _.each(setup.subscribe, function(handler, key) {
265
- bind(built, key, handler);
266
- });
267
-
268
- setup.initialize.call(built, mode);
283
+ // experimental...
284
+ instance(def, self, mode);
285
+ // setup = def.setup;
286
+ // built = def(self, self.context);
287
+ // built.connect(setup.connected);
288
+ //
289
+ // _.each(setup.subscribe, function(handler, key) {
290
+ // bind(built, key, handler);
291
+ // });
292
+ //
293
+ // setup.initialize.call(built, mode);
269
294
  set.push(id);
270
295
  }
271
296
  });
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.7
4
+ version: 0.0.8
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-16 00:00:00.000000000 Z
12
+ date: 2012-05-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: transcendent views
15
15
  email: